django amdin是django提供的一个后台管理页面,改管理页面提供完善的html和css,使得你在通过Model创建完数据库表之后,就可以对数据进行增删改查,而使用django admin 则需要以下步骤:
1、创建后台管理员
1
|
python manage.py createsuperuser |
2、配置后台管理url
1
|
url(r ‘^admin/‘ , include(admin.site.urls)) |
3、注册和配置django admin 后台管理页面
a、在admin中执行如下配置
1
2
3
4
5
6
7
8
|
from django.contrib import admin from app01 import models admin.site.register(models.UserType) admin.site.register(models.UserInfo) admin.site.register(models.UserGroup) admin.site.register(models.Asset) |
b、设置数据表名称
1
2
3
4
5
6
|
class UserType(models.Model): name = models.CharField(max_length = 50 ) class Meta: verbose_name = ‘用户类型‘ verbose_name_plural = ‘用户类型‘ |
c、打开表之后,设定默认显示,需要在model中作如下配置
1
2
3
4
5
|
class UserType(models.Model): name = models.CharField(max_length = 50 ) def __unicode__( self ): return self .name |
1
2
3
4
5
6
7
8
9
10
11
12
|
from django.contrib import admin from app01 import models class UserInfoAdmin(admin.ModelAdmin): list_display = ( ‘username‘ , ‘password‘ , ‘email‘ ) admin.site.register(models.UserType) admin.site.register(models.UserInfo,UserInfoAdmin) admin.site.register(models.UserGroup) admin.site.register(models.Asset) |
d、为数据表添加搜索功能
1
2
3
4
5
6
7
8
9
10
11
12
|
from django.contrib import admin from app01 import models class UserInfoAdmin(admin.ModelAdmin): list_display = ( ‘username‘ , ‘password‘ , ‘email‘ ) search_fields = ( ‘username‘ , ‘email‘ ) admin.site.register(models.UserType) admin.site.register(models.UserInfo,UserInfoAdmin) admin.site.register(models.UserGroup) admin.site.register(models.Asset) |
e、添加快速过滤
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from django.contrib import admin from app01 import models class UserInfoAdmin(admin.ModelAdmin): list_display = ( ‘username‘ , ‘password‘ , ‘email‘ ) search_fields = ( ‘username‘ , ‘email‘ ) list_filter = ( ‘username‘ , ‘email‘ ) admin.site.register(models.UserType) admin.site.register(models.UserInfo,UserInfoAdmin) admin.site.register(models.UserGroup) admin.site.register(models.Asset) |
原文:https://www.cnblogs.com/icemonkey/p/10507790.html