如今正式開始博客開發html
一、安裝django1.4python
若是你使用的是fedoraDVD版,安裝時選擇了web開發組建,這一步能夠省略,由於它自帶django環境mysql
django下載地址 https://www.djangoproject.com/download/ 這裏咱們選擇最新版web
而後在終端下打開下載目錄ajax
tar xzvf Django-*.tar.gz 。 cd Django-* 。 sudo python setup.py install
若是系同時windowsql
解壓後再控制檯進入解壓後的目錄數據庫
python setup.py install
測試安裝django
打開Python的交互解釋器瀏覽器
若是出現如下內容,安裝成功!服務器
>>> import django >>> django.VERSION (1, 4, 1, 'final', 0)
二、新建project
打開終端 輸入
django-admin startproject blog
有些須要輸入
django-admin.py startproject blog
你會發現主文件夾下多出一個目錄 blog
目錄結構爲
blog/ manage.py blog/ __init__.py settings.py urls.py wsgi.py
manage.py :一種命令行工具,可以讓你以多種方式與該 Django 項目進行交互。 鍵入python manage.py help,看一下它能作什麼。
__init__.py :讓 Python 把該目錄當成一個開發包 (即一組模塊)所需的文件。 這是一個空文件,通常你不須要修改它
settings.py :該 Django 項目的設置或配置。 查看並理解這個文件中可用的設置類型及其默認值
urls.py:django項目的URL設置。 可視其爲你的django網站的目錄
wsgi.py: An entry-point for WSGI-compatible webservers to serve your project.See How to deploy with WSGI for more details.
具體使用方法參考 文檔 https://docs.djangoproject.com/en/1.4/intro/tutorial01/
運行服務器
在終端打開項目目錄 輸入
python manage.py runserver
Validating models... 0 errors found Django version 1.4.1, using settings 'blog.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
出現以上選項說明運行服務器成功
訪問 http://127.0.0.1:8000/ 你將看到
三、新建blogapp
在終端打開項目目錄輸入
python manage.py startapp sblog
如今新建好了一個名爲sblog的博客應用
sblog/ __init__.py models.py tests.py views.py
這個目錄包含了這個app的模型和視圖
四、models的配置
由於使用app必須用到數據庫,如今咱們配置一下數據庫 打開setting.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': '/home/gs/blog/datas/mydata.db', # 這裏是我數據庫文件存放的目錄,你應該替換成你本身的. '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. } }
由於python自帶sqlite3,爲了方便咱們就直接使用。
其它數據庫的配置參見 https://docs.djangoproject.com/en/1.4/ref/databases/
如今咱們配置models.py
from django.db import models class Tag(models.Model): """docstring for Tags""" tag_name = models.CharField(max_length=20, blank=True) create_time = models.DateTimeField(auto_now_add=True) def __unicode__(self): return self.tag_name class Author(models.Model): """docstring for Author""" name = models.CharField(max_length=30) email = models.EmailField(blank=True) website = models.URLField(blank=True) def __unicode__(self): return u'%s' % (self.name) class Blog(models.Model): """docstring for Blogs""" caption = models.CharField(max_length=50) author = models.ForeignKey(Author) tags = models.ManyToManyField(Tag, blank=True) content = models.TextField() publish_time = models.DateTimeField(auto_now_add=True) update_time = models.DateTimeField(auto_now=True) def __unicode__(self): return u'%s %s %s' % (self.caption, self.author, self.publish_time)
安裝 models
首先修改setting.py
MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.locale.LocaleMiddleware', # Uncomment the next line for simple clickjacking protection: # 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin:'django.contrib.admin', # Uncomment the next line to enable admin documentation: 'django.contrib.admindocs', 'sblog', )
而後,用下面的命令對校驗模型的有效性:
python manage.py validate
validate 命令檢查你的模型的語法和邏輯是否正確。 若是一切正常,你會看到 0 errors found 消息。 若是有問題,它會給出很是有用的錯誤信息來幫助你 修正你的模型。
最後
python manage.py syncdb
你將看到相似與下面的內容
Creating tables ... Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_user_permissions Creating table auth_user_groups Creating table auth_user Creating table django_content_type Creating table django_session Creating table django_site Creating table django_admin_log Creating table sblog_tag Creating table sblog_author Creating table sblog_blog_tags Creating table sblog_blog
由於咱們修改setting.py時將
'django.contrib.admin', 'django.contrib.admindocs',
註釋去掉了,因此在 執行
python manage.py syncdb
時會 出現You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): 讓咱們新建用戶用於admin管理 ,建立用戶就能夠了五、admin的配置使用修改blog 目錄下 urls.py 添加
from django.contrib import admin admin.autodiscover()
在 patterns 添加 (若是一直沒改過該文件的話 只要將這兩行註釋去掉就能夠了)
url(r'^admin/doc/', include('django.contrib.admindocs.urls')), url(r'^admin/', include(admin.site.urls)),
此時 打開 http://127.0.0.1:8000/admin/
若是你發現咱們新建的sblog並無出現,恭喜你,你有很強的觀察能力,很細心,很。。。。
我仍是接着說怎麼用admin管理咱們的sblog吧。
首先再sblog目錄下新建admin.py 添加如下內容 再刷新admin頁面 將會有驚喜哦
#!/usr/bin/python # -*- coding: utf-8 -*- from django.contrib import admin from sblog.models import Author, Blog, Tag class AuthorAdmin(admin.ModelAdmin): """docstring for AuthorAdmin""" list_display = ('name', 'email', 'website') search_fields = ('name',) class BlogAdmin(admin.ModelAdmin): """docstring for BlogAdmin""" list_display = ('caption', 'id', 'author', 'publish_time') list_filter = ('publish_time',) date_hierarchy = 'publish_time' ordering = ('-publish_time',) filter_horizontal = ('tags',) # raw_id_fields = ('author',) # 它是一個包含外鍵字段名稱的元組,它包含的字段將被展示成`` 文本框`` ,而再也不是`` 下拉框`` 。 admin.site.register(Author, AuthorAdmin) admin.site.register(Blog, BlogAdmin) admin.site.register(Tag)
其中 AuthorAdmin 和 BlogAdmin 是 自定義ModelAdmi類 用於自定義admin顯示
list_display = ('caption', 'id', 'author', 'publish_time') 表示 按照 caption id author publish_time 顯示 另外,點擊每一個列的列頭能夠對那列進行排序。
search_fields = ('name',) 刷新瀏覽器,你會在頁面頂端看到一個查詢欄。咱們剛纔所做的修改列表頁面,添加了一個根據姓名查詢的查詢框
list_filter = ('publish_time',) 用於在右邊生成一個過濾器,按照發表時間過濾
date_hierarchy = 'publish_time' 也是時間過濾器 修改好後,頁面中的列表頂端會有一個逐層深刻的導航條,它從可用的年份開始,而後逐層細分到月乃至日。
ordering = ('-publish_time',) 按照發表時間排序 默認是從前日後排序 加‘-’表示將最近發表的放到前面,從後往前倒序排列
filter_horizontal = ('tags',) 用於多對多字段顯示,出現一個精巧的JavaScript過濾器,它容許你檢索選項,而後將選中的tag從Available框移到Chosen框,還能夠移回來
其它一些自定義方法參見文檔吧 https://docs.djangoproject.com/en/1.4/ref/contrib/admin/
到如今爲止,咱們已經可使用admin管理咱們的博客