5-后台管理-1
准备工作
1、settings.py配置
TEMPLATES中的context_processors加入'django.contrib.auth.context_processors.auth',#用来在模板中访问用户和权限的上下文处理器 'django.contrib.messages.context_processors.messages',#用来支持消息应用的上下文处理器
2、创建用户并授予访问权限
按照前面的文章创建超级用户,但是注意,因为超级用户具有所有的权限,所以存在安全隐患
model注册
1、admin.py中申明
from django.contrib import admin # 在此处注册您的模型. from firstApp.models import Topic,Comment admin.site.register([Topic,Comment])
访问http://127.0.0.1:8000/admin/可看到
自定义名字
刷新页面
点击可对model对象进行增删改查等操作,自动生成或者自增长的不显示,每一个对应字段还可以查看历史(右上角),相对应的改变可在数据库中的django_admin_log表中有详细数据
id:序号;action_time:修改/创建时间;object_id:对应的序号;repr:就是我们在虚拟环境(python manage..py shell):不规定查询字段时查到的数据;flag:2位更新,1为增加;content_type_id:对应django_content_type表
使用ModelAdmin自定义管理后台
model注册
前面讲到将model注册到admin中,但那是最简单的方式
所以我们先简单的用第二种方法注册
# 在此处注册您的模型 from firstApp.models import Topic,Comment #方式一 #admin.site.register([Topic,Comment]) #方式二 #class TopicAdmin(admin.ModelAdmin): #pass #class CommentAdmin(admin.ModelAdmin): #pass #django规定,每一个model只能注册一次,不然会报异常,所以需要把方式一注释 #admin.site.register(Topic,TopicAdmin) #admin.site.register(Comment,CommentAdmin)
#方式三:装饰器注册 @admin.register(Topic) class TopicAdmin(admin.ModelAdmin): pass @admin.register(Comment) class CommentAdmin(admin.ModelAdmin): pass
ModelAdmin属性
1、利用actions丰富你的多选操作
@admin.register(Topic) class TopicAdmin(admin.ModelAdmin):
actions = ['onlines','offonlines']#actions属性定义批量上线和批量下线动作
def onlines(self,request,queryset):#对动作的详细情况进行描述,使用request发出请求,调用QuerySet方法对model操作(前面讲过QuerySet方法) update_isonline=queryset.update(is_online=True)#修改字段并返回修改数量 self.message_user(request,'%s topic online'%update_isonline)#message_user提示信息
onlines.short_description = u'上线所选的 %s'%Topic._meta.verbose_name#short_description给当前行为一个描述
def offonlines(self,request,queryset): update_isonline=queryset.update(is_online=False) self.message_user(request,'%s topic online'%update_isonline) offonlines.short_description = u'下线所选的 %s'%Topic._meta.verbose_name#short_description给当前行为一个描述
2、利用你的list_display修改显示的布局(字段)
list_display = ['title','content','is_online','user','create_time','update_time']#正常情况下,list_diplay会将第一个字段作为link
作如下修改,list_display也可以用作函数上
list_display = ['title','topic_content','topic_is_online','user','create_time','update_time'] def topic_is_online(self,obj):#判断是否在线, return u'在线' if obj.is_online else u'退线' topic_is_online.short_description=u'是否在线' def topic_content(self,obj): return obj.content[:20]#切片处理,只显示前20个字 topic_content.short_description=u'文章简略内容'
对时间进行修改,使时间看起来更加直观
list_display = ['topic_title','topic_content','topic_is_online','topic_user','topic_create_time','topic_update_time'] def topic_create_time(self,obj): loctiontime=time.localtime(time.time()) if obj.create_time.year==loctiontime.tm_year: if obj.create_time.month==loctiontime.tm_mon: if obj.create_time.day==loctiontime.tm_mday: return u'今天' elif obj.create_time.day>=loctiontime.tm_mday: return u'时间错误' else: if loctiontime.tm_mon - obj.create_time.day > 30: return repr(loctiontime.tm_mon - obj.create_time.month)+u'月前' else: return repr(loctiontime.tm_mday - obj.create_time.day)+ u'天前' elif obj.create_time.month>=loctiontime.tm_mon: return u'时间错误' else: if loctiontime.tm_mon-obj.create_time.month>12: return repr(loctiontime.tm_year - obj.create_time.year)+ u'年前' else: return repr(loctiontime.tm_mon-obj.create_time.month)+ u'月前' elif obj.create_time.year>=loctiontime.tm_year: return u'时间错误' else: return repr(loctiontime.tm_year-obj.create_time.year)+ u'年前' topic_create_time.short_description=u'创建时间'
def topic_update_time(self,obj): loctiontime = time.localtime(time.time()) if obj.update_time.year == loctiontime.tm_year: if obj.update_time.month == loctiontime.tm_mon: if obj.update_time.day == loctiontime.tm_mday: return u'今天' elif obj.update_time.day >= loctiontime.tm_mday: return u'时间错误' else: if loctiontime.tm_mon - obj.update_time.day > 30: return repr(loctiontime.tm_mon - obj.update_time.month) + u'月前' else: return repr(loctiontime.tm_mday - obj.update_time.day) + u'天前' elif obj.update_time.month >= loctiontime.tm_mon: return u'时间错误' else: if loctiontime.tm_mon - obj.update_time.month > 12: return repr(loctiontime.tm_year - obj.update_time.year) + u'年前' else: return repr(loctiontime.tm_mon - obj.update_time.month) + u'月前' elif obj.update_time.year >= loctiontime.tm_year: return u'时间错误' else: return repr(loctiontime.tm_year - obj.update_time.year) + u'年前' topic_update_time.short_description=u'修改时间'