Django自动生成Admin后台管理页面
参考资料:
https://docs.djangoproject.com/en/1.3/intro/tutorial02/#activate-the-admin-site
其它说明:
1. 有时候想根据己有数据库生成Model,Django也是可以做到的:python manage.py inspectdb >models.py
上回介绍了Django在windows下的环境搭建,今天介绍Django的杀手锏级应用:Admin的生成。为了能够讲明白,下面简要说一下环境:Django版本:Django1.1.1 简单示例:Blog 数据库:sqlite3
1,新建一个Project
只要你上回你能顺利配置好Django1.1.1的环境,你现在可以打开Dos控制台,来到你想安放Project的目录下;我把Project(命名为v111)暂且放到study_django目录下,于是Dos下输入指令:
d: ,回车
cd study_django ,回车
这时来到了想到的目录下,接着新建Project
django-admin.py startproject v111 ---(django-admin.py是django的全局命令,我们上回已经把它加入系统变量里了,startproject 表示新建工程,v111是工程的名称)
这时你在目录下,可以看到有文件生成了,比如:__init__.py manage.py settings.py urls.py 这四个python文件,这就是整个Project的全部文件,manage.py是主角,我们之后就使用它生成自己想要的东西了,django-admin.py这个全局模块,当我们新建完毕Project,就很少用到它了,先忘记它吧。
Project算是建立好了,在Project里新建个App (App,暂且叫应用吧,一个Project里可以放多个独立的应用A,Django提倡App的热插拔,很好的设计理念),进入刚才新建的Project里
cd v111 回车
来到了Project的目录下,
manage.py startapp blog 回车
这时打开Project的目录 发现了一个文件夹blog,里面有 __init__.py models.py test.py urls.py四个文件,这就是一个独立的blog App了。
太好了,整个Project就这么几个文件, 两行的django命令
django-admin.py startproject v111
manage.py startapp blog
就可以完成了,都简单不过的了。接下来我们可以集中精力,编写自己的网站了,借助Python简单的特点,下来的步骤也不会写很多代码。
2 设置一下Project全局变量,就是让Project更好地跑我们的blog应用。
首先在Project目录下,找到settings.py 指定数据库:
DATABASE_ENGINE
=
"
sqlite3
"
DATABASE_NAME = " study_django_v111.db3 "
DATABASE_USER = ""
DATABASE_PASSWORD = ""
DATABASE_HOST = ""
DATABASE_PORT = ""
DATABASE_NAME = " study_django_v111.db3 "
DATABASE_USER = ""
DATABASE_PASSWORD = ""
DATABASE_HOST = ""
DATABASE_PORT = ""
以上的代码:
DATABASE_ENGINE = "sqlite3"
是指定数据类型,我们使用内置的 sqlite3
DATABASE_NAME = "study_django_v111.db3"
是数据库的名字,我们指定的是 study_django_v111.db3
然后把blog和admin应用 插到Project里
INSTALLED_APPS
=
(
" django.contrib.auth " ,
" django.contrib.contenttypes " ,
" django.contrib.sessions " ,
" django.contrib.sites " ,
" django.contrib.admin " ,
" blog " ,
)
" django.contrib.auth " ,
" django.contrib.contenttypes " ,
" django.contrib.sessions " ,
" django.contrib.sites " ,
" django.contrib.admin " ,
" blog " ,
)
INSTALLED_APPS
本来就用一些基本的应用,我们只需写入两行代码
"django.contrib.admin",
"blog",
Project基本的全局变量已经设置好了.
接下来关键的东西是编写blog的实体,在blog/models.py下,写我们需要的实体字段:
#
coding=utf-8
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Blog(models.Model):
title = models.CharField(u " 标题 " ,max_length = 60 )
content = models.TextField(u " 正文 " )
user = models.ForeignKey(User,verbose_name = u " 作者 " )
category = models.CharField(u " 类型 " ,max_length = 20 )
status = models.NullBooleanField(u " 可用 " )
created_at = models.DateTimeField(u " 建立时间 " ,auto_now_add = True)
updated_at = models.DateTimeField(u " 修改时间 " ,auto_now = True)
class Meta:
verbose_name = u " A.博客 "
verbose_name_plural = u " A.博客列表 "
ordering = ( " -created_at " ,)
get_latest_by = " created_at "
def __unicode__ (self):
return self.title
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Blog(models.Model):
title = models.CharField(u " 标题 " ,max_length = 60 )
content = models.TextField(u " 正文 " )
user = models.ForeignKey(User,verbose_name = u " 作者 " )
category = models.CharField(u " 类型 " ,max_length = 20 )
status = models.NullBooleanField(u " 可用 " )
created_at = models.DateTimeField(u " 建立时间 " ,auto_now_add = True)
updated_at = models.DateTimeField(u " 修改时间 " ,auto_now = True)
class Meta:
verbose_name = u " A.博客 "
verbose_name_plural = u " A.博客列表 "
ordering = ( " -created_at " ,)
get_latest_by = " created_at "
def __unicode__ (self):
return self.title
为了简单说明,整个models模块就是一个实体类Blog。
在blog文件夹了,新建admin.py模块(blog/admin.py) ,在该模块下编写代码,让Django自动生成Admin应用,写入代码如下:
#
coding=utf-8
from django.contrib import admin
from models import *
class BlogAdmin ( admin.ModelAdmin ) :
list_display = ( " user " , " title " , " category " , " status " , " created_at " , " updated_at " ,)
list_display_links = ( " title " ,)
list_filter = ( " user " , " title " , " status " , " created_at " , " updated_at " ,)
ordeing = ( " -created_at " , " -updated_at " ,)
search_fields = ( " title " , " user " , " content " ,)
admin.site.register ( Blog, BlogAdmin )
from django.contrib import admin
from models import *
class BlogAdmin ( admin.ModelAdmin ) :
list_display = ( " user " , " title " , " category " , " status " , " created_at " , " updated_at " ,)
list_display_links = ( " title " ,)
list_filter = ( " user " , " title " , " status " , " created_at " , " updated_at " ,)
ordeing = ( " -created_at " , " -updated_at " ,)
search_fields = ( " title " , " user " , " content " ,)
admin.site.register ( Blog, BlogAdmin )
这时该到收尾工作了,我们回到Dos控制台,执行命令:
manage.py syncdb 回车
Django就会根据你刚才的 编写的models.py下的Blog实体类生成 数据表,同时也会生成一些基本的表,就看Project下的INSTALLED_APPS插入了什么App。
回车后你会看到控制台表的全部过程,这个不用我们操心,就全部交个Django来做好了,但是生成过程中,Django需要输入一个后台的超级管理员账号,
出来Would you like to create one now? (yes/no):的时候,输入 yes 回车,按照指令提示,完成新建工作即可。
我暂且这么起名:管理员 baoyalv 密码 pwd123 Email b@163.com
显示的语句如下:
D:\study_django\v111>manage.py syncdb
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Creating table blog_blog
You just installed Django"s auth system, which means you don"t have any superuse
rs defined.
Would you like to create one now? (yes/no): yes
Username: baoyalv
E-mail address: b@163.com
Password:
Password (again):
Superuser created successfully.
Installing index for auth.Permission model
Installing index for auth.Message model
Installing index for admin.LogEntry model
Installing index for blog.Blog model
等Django执行完了以上的工作,我们在控制台输入 :manage.py runserver 回车
来启动整个Project,当Django启动了内置的web 服务器,我们就可以浏览我们的admin 页面了:
打开浏览器,在地址栏上 输入地址:http://127.0.0.1:8000/admin
怎么样,登录页面出来了吧,用刚才的我们新建的管理员账号登陆进去,你就能看到 blog 和user group的相关模块
后台
管理了。
![](http://pic.cnthub.com/NewsPic//M0/S225/225307CB-2.jpg)
转载请注明原地址:
http://baoyalv.com/blog/baoyalv/other/2010/03/06/26