【Django tutorial 3】

9.添加管理员界面

    首先确认,在 INSTALLED_APPS中是否包含 django.contrib.admin

    如果没有,请添加,并执行命令:python manage.py syncdb

    最后按照如下内容修改 urls.py

from django.conf.urls.defaults import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),    
    # url(r'^mysite/', include('mysite.foo.urls')),    
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

    执行命令:python manage.py runserver,打开http://127.0.0.1:8000/admin/ 即可看到管理员的登陆界面。使用在上一节中创建的superuser和密码登陆即可。

10. 创建投票程序

    在Poll文件夹下创建文件 admin.py在文件中写入如下代码:

from polls.models import Poll
from django.contrib import admin

admin.site.register(Poll)
    注意:仅当增加或删除文件时才需要重启python manage.py runserver 如果仅仅是更改文件,无需重启服务

    为了更加美观、并且能够自定义各项内容:可以将上述的 admin.site.register(Poll)改成:

class PollAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date']}),
    ]

admin.site.register(Poll, PollAdmin)
或者也可以使用HTML中的一些特性改成如下代码:自己可以登陆浏览器观察效果。

class PollAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
11. 增加相关联对象

    增加 choice对象:

from polls.models import Choice

admin.site.register(Choice)
换一种更高效的方式增加对象

class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3

class PollAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]
    list_display = ('question', 'pub_date', 'was_published_today')
    list_filter = ['pub_date']
    date_hierarchy = 'pub_date'
admin.site.register(Poll, PollAdmin)
其中 ChoiceInline参数可以改为TabularInline,这样可以改变外观。随你喜欢。

为了使页面更加易于观看,可以对model.py代码做如下修改:

def was_published_today(self):
    return self.pub_date.date() == datetime.date.today()
was_published_today.short_description = 'Published today?'

12. 定制管理员登陆界面外观

在mysite/settings.py中添加template目录即可。

TEMPLATE_DIRS = (
    'E/pythoncode/templates/', # Change this to your own directory.
)
从Django的默认安装路径(django/contrib/admin/templates)中,拷贝admin/base_site.html至你需要的位置(e.g. E\pythoncode\templates)。注意:需要拷贝admin这个文件夹一起。打开这个template文件(base_site.html),可以把其中的 Django内容换成自己网站的名字。

关于应用在管理员用户中的位置,默认其是按照首字母排序,如果想更改顺序,可以像增加base_site.html一样增加一个template文件 index.html,同样放在admin文件夹下面。


请继续阅读 tutorial4








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值