Django后台:少许代码,实现强大的网站后台

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样式的后台页面.

相关文章
相关标签/搜索