接着上一部分的内容,进入我们mysite目录,使用下面的命令,运行开发服务器,然后在浏览器打开网页 http://127.0.0.1:8000/admin/
python manage.py runserver
输入我们在创建数据库的时候的用户名和密码,用户名如果默认的话,是你当前机器的登录名,如果忘了,重新来一次吧。
进入后的页面是看不见我们的app的,就是看不见我们的投票系统。
我们在polls/admin.py这个文件里面修改如下代码:
from django.contrib import admin from polls.models import Poll admin.site.register(Poll)
刷新我们的页面,可以看见Polls了
点击What's new 进入
我们来定制自己的格式页面
同样修改admin.py这个文件如下:
from django.contrib import admin from polls.models import Poll class PollAdmin(admin.ModelAdmin): fields = ['pub_date', 'question'] admin.site.register(Poll, PollAdmin)
刷新页面可以看见时间和问题已经顺序发生了变化了。
在改一下:
from django.contrib import admin from polls.models import Poll class PollAdmin(admin.ModelAdmin): #fields = ['pub_date', 'question'] fieldsets = [ (None, {'fields':['question']}), ('Date information', {'fields':['pub_date']}) ] admin.site.register(Poll, PollAdmin)
刷新看看是不是不一样了?
这样呢:
from django.contrib import admin from polls.models import Poll class PollAdmin(admin.ModelAdmin): #fields = ['pub_date', 'question'] fieldsets = [ ('Question', {'fields':['question']}), ('Date information', {'fields':['pub_date']}) ] admin.site.register(Poll, PollAdmin)
在改改:
from django.contrib import admin from polls.models import Poll class PollAdmin(admin.ModelAdmin): #fields = ['pub_date', 'question'] fieldsets = [ ('Question', {'fields':['question']}), ('Date information', {'fields':['pub_date'], 'classes':['collapse']}) ] admin.site.register(Poll, PollAdmin)
其实collapse就是一个隐藏功能,比如有的表单非常长,就可以使用这个了。
现在我们来看看我们的选项,投票的选项,同样先注册了。
from django.contrib import admin from polls.models import Poll, Choice class PollAdmin(admin.ModelAdmin): #fields = ['pub_date', 'question'] fieldsets = [ ('Question', {'fields':['question']}), ('Date information', {'fields':['pub_date'], 'classes':['collapse']}) ] admin.site.register(Poll, PollAdmin) admin.site.register(Choice)
点击增加后:
有没有发现,每次需要增加一个选项,就得回主页点击增加choice,好麻烦,好在django提供了便利,修改admin.py文件如下:
其实选项只和问题有关,不要单独列举出来就好了。
from django.contrib import admin from polls.models import Poll, Choice class ChoiceInline(admin.StackedInline): model = Choice extra = 3 class PollAdmin(admin.ModelAdmin): #fields = ['pub_date', 'question'] fieldsets = [ ('Question', {'fields':['question']}), ('Date information', {'fields':['pub_date'], 'classes':['collapse']}) ] inlines = [ChoiceInline] admin.site.register(Poll, PollAdmin) #admin.site.register(Choice)
感觉这样出来的界面很占用页面空间的话,修改修改:
from django.contrib import admin from polls.models import Poll, Choice #class ChoiceInline(admin.StackedInline): class ChoiceInline(admin.TabularInline): model = Choice extra = 3 class PollAdmin(admin.ModelAdmin): #fields = ['pub_date', 'question'] fieldsets = [ ('Question', {'fields':['question']}), ('Date information', {'fields':['pub_date'], 'classes':['collapse']}) ] inlines = [ChoiceInline] admin.site.register(Poll, PollAdmin) #admin.site.register(Choice)
好多了。
现在让我们来定制我们的admin列表:
记得有个函数叫was_published_recently()吧,加进去看看
from django.contrib import admin from polls.models import Poll, Choice #class ChoiceInline(admin.StackedInline): class ChoiceInline(admin.TabularInline): model = Choice extra = 3 class PollAdmin(admin.ModelAdmin): #fields = ['pub_date', 'question'] fieldsets = [ ('Question', {'fields':['question']}), ('Date information', {'fields':['pub_date'], 'classes':['collapse']}) ] inlines = [ChoiceInline] list_display = ('question', 'pub_date', 'was_published_recently') admin.site.register(Poll, PollAdmin) #admin.site.register(Choice)
oh,这是我昨天加入的,所以为false了。
可以看见,你可以点击question或者date published进行排序的,但是我们的函数那个是无法排序的,想要它也可排序,进行如下操作吧:
我们增加一个filter:(admin.py文件)
from django.contrib import admin from polls.models import Poll, Choice #class ChoiceInline(admin.StackedInline): class ChoiceInline(admin.TabularInline): model = Choice extra = 3 class PollAdmin(admin.ModelAdmin): #fields = ['pub_date', 'question'] fieldsets = [ ('Question', {'fields':['question']}), ('Date information', {'fields':['pub_date'], 'classes':['collapse']}) ] inlines = [ChoiceInline] list_display = ('question', 'pub_date', 'was_published_recently') list_filter = ['pub_date'] #这里也可以是question,就会安装question进行排序了 admin.site.register(Poll, PollAdmin) #admin.site.register(Choice)
还可以增加一个搜索框:
from django.contrib import admin from polls.models import Poll, Choice #class ChoiceInline(admin.StackedInline): class ChoiceInline(admin.TabularInline): model = Choice extra = 3 class PollAdmin(admin.ModelAdmin): #fields = ['pub_date', 'question'] fieldsets = [ ('Question', {'fields':['question']}), ('Date information', {'fields':['pub_date'], 'classes':['collapse']}) ] inlines = [ChoiceInline] list_display = ('question', 'pub_date', 'was_published_recently') list_filter = ['pub_date'] search_fields = ['question'] #search_fields = ['pub_date'] admin.site.register(Poll, PollAdmin) #admin.site.register(Choice)
可以让函数那列也更改下吧:
from django.db import models import datetime from django.utils import timezone class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __unicode__(self): return self.question def was_published_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days = 1) was_published_recently.admin_order_field = 'pub_date' was_published_recently.boolean = True was_published_recently.short_description = 'test' class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) def __unicode__(self): return self.choice_text
当然最后安装原始教程,我们还是改回去
from django.db import models import datetime from django.utils import timezone class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __unicode__(self): return self.question def was_published_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days = 1) was_published_recently.admin_order_field = 'pub_date' was_published_recently.boolean = True was_published_recently.short_description = 'Published recently?' class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) def __unicode__(self): return self.choice_text
注意了,我们目前poll里面只有What's new这个问题,你可以add一个,便于测试,用后删除就是了。