Django实践:个人博客系统(第八章 admin的配置)

如何配置使用django admin?
1、在INSTALLED_APPS中添加django.contrib.admin
2、配置urls.py
url(r'^admin/', include(admin.site.urls))
3、在admin注册的Model,(默认方式和自定义方式),注意model中关于admin的一些配置

fields\exclude

自定义管理类AritcleAmin,并添加在注册中。作用:添加文章的时候只显示fields中的三列。exclude则出去相应列

from django.contrib import admin
from models import *
# Register your models here.

class AritcleAdmin(admin.ModelAdmin):

    fields = ('title', 'desc', 'content')


admin.site.register(User)
admin.site.register(Tag)
admin.site.register(Article, AritcleAdmin)
admin.site.register(Category)
admin.site.register(Comment)
admin.site.register(Links)
admin.site.register(Ad)

 


fieldsets

# -*- coding:utf-8 -*-
from django.contrib import admin
from models import *
# Register your models here.

class AritcleAdmin(admin.ModelAdmin):

    fieldsets = (
        (None, {
            'fields': ('title', 'desc', 'content',)
        }),
        ('高级设置',{
            'classes': ('collapse',),
            'fields': ('click_count', 'is_recommend',)
        }),
    )


admin.site.register(User)
admin.site.register(Tag)
admin.site.register(Article, AritcleAdmin)
admin.site.register(Category)
admin.site.register(Comment)
admin.site.register(Links)
admin.site.register(Ad)

效果Django实践:个人博客系统(第八章 admin的配置)_第1张图片


list_display

# -*- coding:utf-8 -*-
from django.contrib import admin
from models import *
# Register your models here.

class AritcleAdmin(admin.ModelAdmin):

    list_display = ('title', 'desc', 'click_count',)

    '''fieldsets = (
        (None, {
            'fields': ('title', 'desc', 'content',)
        }),
        ('高级设置',{
            'classes': ('collapse',),
            'fields': ('click_count', 'is_recommend',)
        }),
    )'''


admin.site.register(User)
admin.site.register(Tag)
admin.site.register(Article, AritcleAdmin)
admin.site.register(Category)
admin.site.register(Comment)
admin.site.register(Links)
admin.site.register(Ad)

效果(默认只显示一列):    

list_display_links

class AritcleAdmin(admin.ModelAdmin):

    list_display = ('title', 'desc', 'click_count',)
    list_display_links = ('title', 'desc', 'click_count',)

效果:在其中添加的列可以链接到编辑页面


list_editable

class AritcleAdmin(admin.ModelAdmin):

    list_display = ('title', 'desc', 'click_count',)
    list_display_links = ('title', 'desc', )
    list_editable = ('click_count',)

效果:使相应列可编辑,但此列不能同时放入list_display_links和list_editable中。添加此属性后右下角会有保存按钮


list_filter
inlines
...

参考资料:
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/

xadmin

扩展阅读:admindocs的使用
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/admindocs/

 

你可能感兴趣的:(Django实践:个人博客系统(第八章 admin的配置))