首页 > 代码库 > Django(admin site)

Django(admin site)

  1. admin site:django自带了admin site,我们需要创建能访问site的用户
    #以交互的方式创建超级用户manage.py createsuperuser
  2. 如果要把model加到admin site,需要在polls/admin.py中执行如下命令:
    from django.contrib import adminfrom polls.models import Questionadmin.site.register(Question)
    然后可以在site中看到polls的modelDjango admin index page, now with polls displayed
  3. 打开admin site的model,会有一个默认form显示model的成员,Editing form for question object
    如果想自定义form可以使用如下代码:
    #polls/admin.pyfrom django.contrib import adminfrom polls.models import Questionclass QuestionAdmin(admin.ModelAdmin):    fields = [pub_date, question_text]admin.site.register(Question, QuestionAdmin)
    form的布局将会变为:
    Fields have been reordered
    如果有许多fields,可以将form按field set进行划分:
    from django.contrib import adminfrom polls.models import Questionclass QuestionAdmin(admin.ModelAdmin):    fieldsets = [        (None,               {fields: [question_text]}),        (Date information, {fields: [pub_date], classes: [collapse]}),    ]

 

Django(admin site)