Django的後臺只須要少許代碼,就能夠實現強大功能.
本文章以Django 1.8.4位版本測試,基於python3.4,Ubuntu 14.10.以root帳戶運行.之後臺添加博客位例子.php
1.新建一個名稱爲blog_project的帳戶,和名爲blog的apppython
#django-admin startproject blog_project #cd blog_project #django-admin startapp blog
2.將blog添加到setting.py文件中的INSTALLED_APPS中nginx
#vim blog_project/setting.py
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', )
3.修改blog文件夾中的models.py,創建數據庫數據庫
# coding:utf-8 from django.db import models class Article(models.Model): title = models.CharField(u'標題', max_length=256) content = models.TextField(u'內容') pub_date = models.DateTimeField(u'發表時間', auto_now_add=True, editable = True) update_time = models.DateTimeField(u'更新時間',auto_now=True, null=True) def __str__ (self):#在Python2中用__unicode__替換__str__ return self.title
4.同步全部的數據表django
#python3 manage.py syncdb #python3 manage.py makemigrations #python3 manage.py migrate
顯示如下內容,下面黃色標記的部分是添加後臺的superuser,添加你本身的帳號就好.固然添加superuser還有別的方法,下面咱們會詳細介紹.vim
#python3 manage.py syncdb
Operations to perform:
Synchronize unmigrated apps: staticfiles, gunicorn, messages
Apply all migrations: sessions, auth, contenttypes, admin
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying sessions.0001_initial... OK服務器
You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'root'): tu
Email address: liyuelumia@live.cn
Password:
Password (again):
Superuser created successfully.
session
# python3 manage.py makemigrations Django1.7版本以上須要運行這兩個命令
Migrations for 'blog':
0001_initial.py:
- Create model Article
app
# python3 manage.py migrate
Operations to perform:
Synchronize unmigrated apps: messages, staticfiles, gunicorn
Apply all migrations: auth, admin, blog, sessions, contenttypes
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying blog.0001_initial... OK
測試
5.添加superuser帳戶
除了上面同步數據時默認添加superuser帳戶的方法外,還有其餘的添加方法.須要運行Django命令.
(1)新建一個用戶名,使用以下命令:
#python3 manage.py createsuperuser
(2)輸入打算使用的登陸名:
Username(leave blank to use 'administrator'):user01
(3)輸入email:
Email address:
(4)輸入密碼,須要輸入兩次,輸入過程當中密碼不顯示:
Password:
Password(again):
(5)當兩次密碼都相同的時候,就會提示superuser建立成功。
Superuser created successfully
6.修改admin.py
進入blog文件夾,修改admin.py文件,編輯內容以下:
from django.contrib import admin from .models import Article admin.site.register(Article)
只須要這三行代碼,就能建立強大的後臺!同時,urls.py中關於admin的url已經默認開啓,因此啓動服務器,就能夠訪問後臺了.
#python3 manage.py runserver
訪問http://localhost:8000/admin/
輸入以前設定的superuser的帳號密碼,就能登陸後臺了.
7.使用nginx部署Django時,有時候會出現後臺樣式丟失的狀況.好比:
出現這種狀況的緣由是,Nginx配置靜態地址錯誤.進入/etc/nginx/sites-available/default文件,添加:
location /static/ {
alias /usr/local/lib/python3.4/dist-packages/django/contrib/admin/static/;
}
這樣刷新頁面,就會顯示帶有CSS樣式的後臺頁面.