• 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的一些配置

    fieldsexclude

    自定义管理类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)

    效果


    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/

  • 相关阅读:
    JDBC
    uml 和 unified process
    关于N9手机第三种交互方式的思考和再设计
    [jQuery插件] jQuery Color Animations颜色动画插件
    Azul发布开源工具jHiccup,为Java提供运行时响应时间分析
    行内元素verticalalign:middle在html5和xhtml1.0及以下版本中的表现差异
    使用maven进行scala项目的构建
    计划FM为人人网提供首个开源Ruby SDK
    Chrome扩展:Run Selected HTML
    Team Foundation Service更新:改善了导航和项目状态速查功能
  • 原文地址:https://www.cnblogs.com/songbird/p/5142695.html
Copyright © 2020-2023  润新知