Django的后台只需要少量代码,就可以实现强大功能.
本文章以Django 1.8.4位版本测试,基于python3.4,Ubuntu 14.10.以root账户运行.以后台添加博客位例子.
1.新建一个名称为blog_project的账户,和名为blog的app
#django-admin startproject blog_project #cd blog_project #django-admin startapp blog
2.将blog添加到setting.py文件中的INSTALLED_APPS中
#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.同步所有的数据表
#python3 manage.py syncdb #python3 manage.py makemigrations #python3 manage.py migrate
显示以下内容,下面黄色标记的部分是添加后台的superuser,添加你自己的账号就好.当然添加superuser还有别的方法,下面我们会详细介绍.
#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: [email protected]
Password:
Password (again):
Superuser created successfully.
# python3 manage.py makemigrations Django1.7版本以上需要运行这两个命令
Migrations for 'blog':
0001_initial.py:
- Create model Article
# 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样式的后台页面.