如何用Python+django 10分鐘內做出一個blog

在suse11.2上使用django1.4.1搞了N久都沒有完成這個使命,最後在window下搞定,因此環境,版本很重要:

windows7 + python2.6 + django1.3.3 + sqlite32.4.1  html

Step1 >>> 創建 project  python

運行: django-admin.py startproject hello mysql

修改 demo/settings.py 用 sqlite3 做爲數據庫,產生一 hello.sqlite3 的數據庫保存數據,再設定 template 目錄等: sql

import os
PROJECT_DIR=os.path.dirname(__file__)
DATABASES = {
    'default': {
        'ENGINE': 'sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'hello.sqlite3',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}
...
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'static')
...
TEMPLATE_DIRS = (
    os.path.join(PROJECT_DIR, 'template'),
)

)INSTALLED_APPS = ( ...
    'django.contrib.admin',
    'hello.blog',  # 這個稍後產生,其實就是在hello目錄下新建app
)
Step2 >>> demo 目錄下建 blog 的 app:

運行:python manage.py startapp blog shell

修改 ./blog/models.py : 數據庫

from django.db import models
from django.contrib import admin

# Create your models here.

class Category(models.Model):
  name = models.CharField(max_length=32)
  def __unicode__(self):
    return self.name
  class Admin:
    pass


class Article(models.Model):
  title         = models.CharField(max_length=64)
  published_at  = models.DateTimeField('date published')
  content       = models.TextField()
  category      = models.ForeignKey(Category)
  def __unicode__(self):
    return self.title
  class Admin:
    pass
    
admin.site.register(Category)
admin.site.register(Article)

備註:生成的blog和settings.py 同級目錄,這一點差點搞死筆者了,囧... django

Step3 >>> 創建數據庫 windows

運行:python manage.py sql blog  瀏覽器

運行:python manage.py syncdb oracle

後者會創建管理者賬號,設置密碼等

修改:hello/urls.py  (去掉某些註釋增長一個url映射)

from django.conf.urls.defaults import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'hello.views.home', name='home'),
    # url(r'^hello/', include('hello.foo.urls')),


    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),


    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    (r'^blog/', include('hello.blog.urls')),
)

先在 ./blog/ 下創建一 urls.py

運行 python manage.py runserver,瀏覽器打開 http://localhost:8000/admin/ 就有可用的前臺了!


Step4 >>> 建模版

hello下建文件和目錄 template/blog/article_detail.html 和 template/blog/article_list.html

article_list.html:


{% if object_list %}
  {% for article in object_list %}
    <div class="article">
      <div class="title"><a href="/blog/{{ article.id }}">{{ article.title }}</a></div>
    </div>
  {% endfor %}
{% else %} 
  <p>對不起沒有文章喔!</p>
{% endif %}
article_detail.html



<div class="article">
  <div class="title">標題: {{ object.title }}</div>
  <div class="pub_date">{{ object.published_at }}</div>
  <div class="content">{{ object.content }}</div>
  <div class="category">發表於: {{ object.category.name }}</div>
</div>
<p><a href="/admin/blog/article/{{ object.id }}">修改</a></p>
<p><a href="/blog">BACK</a></p>

備註:兩個html文件須要使用utf8編碼格式保存,不然出現 UnicodeDecodeError 

再次運行 python manage.py runserver

筆者的目錄結構以下,僅供參考:


文件夾 PATH 列表
卷序列號爲 00000002 0C01:3598
D:\DJANGO_PROJ\HELLO
│  hello.sqlite3
│  manage.py
│  settings.py
│  settings.pyc
│  start.bat
│  start.err.log
│  start.run.log
│  urls.py
│  urls.pyc
│  __init__.py
│  __init__.pyc
│  啓動.bat
│  
├─blog
│      models.py
│      models.pyc
│      tests.py
│      urls.py
│      urls.pyc
│      views.py
│      __init__.py
│      __init__.pyc
│      
├─static
└─template
    └─blog
            article_detail.html
            article_list.html


參考文獻:

http://n23.appspot.com/blog/post/3412 【原文】

http://www.doc88.com/p-004801243893.html 【有截圖】

http://doc.open-open.com/view/91a54afc2edb4bcfb05fbb275e308cda 【最給力】

相關文章
相關標籤/搜索