Django 有着强大而又及其易用的admin后台,在这里,你可以轻松实现复杂代码实现的功能,如搜索,筛选,分页,题目可编辑,多选框.
简单到,一行代码就可以实现一个功能,而且模块之间耦合得相当完美.
不信,一起来看看吧!?用Django实现管理书籍的系统,并能在前台界面对书籍进行增删查改,筛选,分页,以及批量查询修改功能.
准备工作
1
|
#准备好你的数据库模型思维导图
|
0.新建一个Django项目,起名为books,并且同时新建一个应用book11
1.首先要设置models模块,根据思维导图,我们知道需要定义3张表,分别是Book,Author,Publisher.
1.1然而,django强大的地方在于,你无需在数据库开辟一张表,ORM的模型,让你只需要关注你要操作的对象.这里用类对象,来替代表,从而使得定义一张数据表Book,就只需要简单的创建Book类对象,即可
1
2
3
4
5
6
7
8
|
#先设计作者Author对象(表)[models.py]
class Author(models.Model): #继承于models.Model这个父类,从而实现多态
first_name = models.CharField(max_length = 32 ) #名字的字段,使用字符串格式,最大长度32
last_name = models.CharField(max_length = 32 )
email = models.EmailField() #email字段,使用email自带的格式
def __unicode__( self ): #定义unicode函数,是为了让对象在实例化的时候,可以返回打印输出它的名字<阿文>.不至于显示为<** object>
return "%s--%s" % ( self .first_name, self .last_name)
|
1
2
3
4
5
6
7
8
9
10
|
#出版社
class Publisher(models.Model):
name = models.CharField(max_length = 64 ,unique = True ) #出版社名称,唯一,是主键
address = models.CharField(max_length = 64 ,unique = True )
city = models.CharField(max_length = 32 )
state_province = models.CharField(max_length = 32 )
country = models.CharField(max_length = 32 )
website = models.URLField() #主页,采用自带的url格式
def __unicode__( self ):
return "%s" % ( self .name)
|
1
2
3
4
5
6
|
#定义一个选项,里面包含3个可选框,用以下面的书籍表publisher_state下拉选择
STATUS_CHOICES = (
( 'checkout' ,u '已出版' ),
( 'dai' ,u '待出版' ),
( 'status' ,u '审核中' ),
)
|
1
2
3
4
5
6
7
8
9
|
#书籍表
class Book(models.Model):
name = models.CharField(max_length = 64 )
authors = models.ManyToManyField(Author) #作者,多对多的关系
publisher = models.ForeignKey(Publisher) #出版社,外键管理到Publisher表
publisher_date = models.DateField(auto_now_add = True )
publisher_state = models.CharField(max_length = 20 ,choices = STATUS_CHOICES,default = 'checkout' ) #出版状态,是一个可选框
def __unicode__( self ):
return "%s--%s" % ( self .name, self .publisher_date)
|
完整的代码:
1.2然后,创建完models后,一定要创建映射文件,并且映射到数据库,否则数据库是不存在的
1
|
python manage.py makemigrations
|
1
|
python manage.py migrate
|
2.调试shell的models模块,使用对象操作增删查改
1
2
3
4
5
6
7
8
9
10
|
##进入shell 模式
python manage.py shell
##导入Publisher对象
from book11.models import Publisher
##查询id=1的queryset赋值给p
p = Publisher.objects.get( id = 1 )
#对的对象进行操作,修改city="changsha",等价于[Publisher.objects.filter(id=1).update(city='changsha')]操作.
p.city = 'changsha'
#需要提交保存,否则不生效
p.save()
|
前后对比,操作数据库字段就像操作对象一样简单!这便是ORM的优势
3.增加一个admin用户
1
2
3
4
5
6
|
G:\git\web\books>python manage.py createsuperuser #增加一个超级用户
Username (leave blank to use 'huan5' ): admin #用户名
Email address: admin@qq.com #邮箱
Password: #输8位密码2次
Password (again):
Superuser created successfully.
|
4.编辑admin.py
4.1将models导入,并且注册网站的Author,Publisher,Boos模块
1
2
3
4
5
6
|
#设置默认编码符
# -*- coding: utf-8 -*-
from book11 import models #导入数据库
admin.site.register(models.Author)
admin.site.register(models.Publisher)
admin.site.register(models.Book)
|
1
2
3
4
|
#启动服务
python manage.py runserver 0.0 . 0.0 : 8086
#并打开浏览器执行
http: / / 127.0 . 0.1 : 8086
|
书名已经成功显示出来
4.2配置一些扩展功能
1
2
3
4
5
6
7
8
9
10
11
12
|
from book11 import models
# 创建一个Bookadmin的modeladmin的子类
class Bookadmin(admin.ModelAdmin):
list_display = ( 'id' , 'name' , 'publisher' , 'publisher_date' , 'publisher_state' )
search_fields = ( 'name' ,)
list_filter = ( 'publisher' , 'publisher_date' ,)
list_per_page = 5
list_editable = ( 'name' , 'publisher_state' ,)
list_select_related = ( 'publisher' ,)
filter_horizontal = ( 'authors' ,)
raw_id_fields = ( 'publisher' ,)
actions = [ 'set_publisher_checkout' , 'set_publisher_dai' , 'set_publisher_status' , 'set_publisher_del' ,]
|
1
2
3
|
admin.site.register(models.Author,Authoradmin) #不要忘了把这些定义好的扩展写进来
admin.site.register(models.Publisher,Publisheradmin)
admin.site.register(models.Book,Bookadmin)
|
1
2
3
|
#配置作者页面的扩展内容
class Authoradmin(admin.ModelAdmin):
list_display = ( 'first_name' , 'last_name' , 'email' )
|
1
2
3
|
#配置出版社的扩展显示
class Publisheradmin(admin.ModelAdmin):
list_display = ( 'name' , 'address' , 'country' ,)
|
4.3为了批量操作,我们给他增加了actions,定义如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
def set_publisher_checkout(modeladmin,request,queryset):
selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME) #选中传入的表单中,勾选的checkbox对应的id集合
models.Book.objects. filter (id__in = selected).update(publisher_state = 'checkout' ) #将所有选中的id对象,修改出版状态为checkout
def set_publisher_dai(modeladmin,request,queryset):
selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects. filter (id__in = selected).update(publisher_state = 'dai' )
def set_publisher_status(modeladmin,request,queryset):
selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects. filter (id__in = selected).update(publisher_state = 'status' )
def set_publisher_del(modeladmin,request,queryset): #########扩展部分,增加对选中的记录今夕删除!###########
selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects. filter (id__in = selected).delete()
set_publisher_checkout.short_description = "设置所有的书籍为--已出版" #为了使界面更加友好,添加别名
set_publisher_status.short_description = "设置所有的书籍为--审核中"
set_publisher_dai.short_description = "设置所有的书籍为--待出版"
set_publisher_del.short_description = "设置所有的书籍为--删除"
|
效果图
调试成功
附上admin.py的全部代码,方便调试不成功的小伙伴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from book11 import models
# Register your models here.
class Bookadmin(admin.ModelAdmin):
list_display = ( 'id' , 'name' , 'publisher' , 'publisher_date' , 'publisher_state' )
search_fields = ( 'name' ,)
list_filter = ( 'publisher' , 'publisher_date' ,)
list_per_page = 5
list_editable = ( 'name' , 'publisher_state' ,)
list_select_related = ( 'publisher' ,)
filter_horizontal = ( 'authors' ,)
raw_id_fields = ( 'publisher' ,)
actions = [ 'set_publisher_checkout' , 'set_publisher_dai' , 'set_publisher_status' , 'set_publisher_del' ,]
def set_publisher_checkout(modeladmin,request,queryset):
selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects. filter (id__in = selected).update(publisher_state = 'checkout' )
def set_publisher_dai(modeladmin,request,queryset):
selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects. filter (id__in = selected).update(publisher_state = 'dai' )
def set_publisher_status(modeladmin,request,queryset):
selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects. filter (id__in = selected).update(publisher_state = 'status' )
def set_publisher_del(modeladmin,request,queryset):
selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects. filter (id__in = selected).delete()
set_publisher_checkout.short_description = "设置所有的书籍为--已出版"
set_publisher_status.short_description = "设置所有的书籍为--审核中"
set_publisher_dai.short_description = "设置所有的书籍为--待出版"
set_publisher_del.short_description = "设置所有的书籍为--删除"
class Authoradmin(admin.ModelAdmin):
list_display = ( 'first_name' , 'last_name' , 'email' )
class Publisheradmin(admin.ModelAdmin):
list_display = ( 'name' , 'address' , 'country' ,)
admin.site.register(models.Author,Authoradmin)
admin.site.register(models.Publisher,Publisheradmin)
admin.site.register(models.Book,Bookadmin)
|
不禁感叹再也没有比django更加简单好用,容易操作的后台了.
总结
Django真的太强大。。
以上就是本文关于Django admin实现图书管理系统菜鸟级教程完整实例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:http://www.yueguangzu.net/?p=1181