Django后台

Django的后台,只需要添加少量代码就可以实现一个强大的后台功能

1、新建项目(略)

2、新建一个APP(略)

3、修改APP的下的models.py

#coding:utf8

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 __unicode__(self):# 在Python3中用 __str__ 代替 __unicode__
    return self.title

如何兼容python2.x和python3.x呢?

# coding:utf-8
from __future__ import unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible

  @python_2_unicode_compatible
  class Article(models.Model):
  title = models.CharField('标题', max_length=256)
  content = models.TextField('内容')
  pub_date = models.DateTimeField('发表时间', auto_now_add=True, editable = True)
  update_time = models.DateTimeField('更新时间',auto_now=True, null=True)
  def __str__(self):
    return self.title

python_2_unicode_compatible 会自动做一些处理去适应python不同的版本,本例中的 unicode_literals 可以让python2.x 也像 python3 那个处理 unicode 字符,以便有更好地兼容性。

 

4、添加APP到settings.py下的INSTALLED_APPS里

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'APP',
)

5、同步数据库

python manage.py syncdb

可以看到:

Creating tables ...

Creating table django_admin_log

Creating table auth_permission

Creating table auth_group_permissions

Creating table auth_group

Creating table auth_user_groups

Creating table auth_user_user_permissions

Creating table auth_user

Creating table django_content_type

Creating table django_session

Creating table blog_article

 

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): yes

Username (leave blank to use 'tu'): tu

Email address:

Password:

Password (again):

Superuser created successfully.

Installing custom SQL ...

Installing indexes ...

Installed 0 object(s) from 0 fixture(s)

如果不提示创建管理员账户,可以手工创建(Django1.9不会自动创建)

python manage.py createsuperuser

6、修改admin.py

进入 APP 文件夹,修改 admin.py 文件(如果没有新建一个),内容如下 

from django.contrib import admin
from .models import Article
admin.site.register(Article)

只需要三行代码,就实现了一个强大的后台

7、启动服务器

Python manage.py runserver 0.0.0.0:80

打开浏览器输入链接地址,输入用户名和密码,就可以看到

点击 Articles,动手输入 添加几篇文章,就可以看到:

 

8、在列表显示与字段相关的其它内容

后台已经基本上做出来了,可是如果我们还需要显示一些其它的fields

from django.contrib import admin
from .models import Article
class ArticleAdmin(admin.ModelAdmin):
  list_display = ('title','pub_date','update_time',)
admin.site.register(Article,ArticleAdmin)

list_display 就是来配置要显示的字段的,当然也可以显示非字段内容,或者字段相关的内容,比如:

class Person(models.Model):
  first_name = models.CharField(max_length=50)
  last_name = models.CharField(max_length=50)
  def my_property(self):
    return self.first_name + ' ' + self.last_name
  my_property.short_description = "Full name of the person"
  full_name = property(my_property)

在admin.py中

from django.contrib import admin
from .models import Article, Person
class ArticleAdmin(admin.ModelAdmin):
  list_display = ('title', 'pub_date', 'update_time',)
class PersonAdmin(admin.ModelAdmin):
  list_display = ('full_name',)
admin.site.register(Article, ArticleAdmin)
admin.site.register(Person, PersonAdmin)


  

posted @ 2016-10-24 09:59  小电脑-滴雨  阅读(303)  评论(0编辑  收藏  举报