Django学习笔记《admin》

这篇博客介绍了如何定制Django的admin界面,包括自定义admin表单、添加相关对象、改变change list显示内容和调整样式。内容涵盖自定义表单字段、区域划分、处理外键关系、增加过滤和搜索选项,以及如何改变admin界面的默认样式,确保应用在多个项目中都能找到所需模板。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于admin 的资源在官网上有:
writing your first Django app part 7
主要内容有:

Customize the admin form

自定义表单的域

#polls/admin.py
from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fields = ['pub_date', 'question_text']

admin.site.register(Question, QuestionAdmin)

划分区域

#polls/admin.py
from django.contrib import admin

from .models import Question


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

admin.site.register(Question, QuestionAdmin)

非常重要的部分。需要强调的是,当前的表必须是别的表的外键

polls/admin.py
from django.contrib import admin

from .models import Choice, Question


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


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]

admin.site.register(Question, QuestionAdmin)

或者这样定义:
ChoiceInline

class ChoiceInline(admin.TabularInline):
    #...

Customize the admin change list

自定义change list 显式的内容
class QuestionAdmin(admin.ModelAdmin):
    # ...
    list_display = ('question_text', 'pub_date', 'was_published_recently')

question_text,pub_date 是Question的字段
而was_published_recently 是Question方法,充分证明了python中的一切都是对象。。

增加过滤项

list_filter = ['pub_date']

增加搜索项

search_fields = ['question_text']

改变admin的样式

admin虽然强大,但是总得改改样子吧。。
不然三天两头只能看见那个鬼样子。

具体的步骤是这样子的,如果admin这个app需要寻找templates的话,它首先会在你规定的templates中寻找,所以我们不需要去修改它的源码。只需要将
djaong/contrib/admin/templates/admin/base_site.html中的文件拷贝到工程的templates目录下,并且将这个templates添加到settings.py中即可。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

例如修改一下/admin/base_site.html这个文件:

{% extends "admin/base.html" %}

{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">it all depends on you</a></h1>
{% endblock %}

{% block nav-global %}{% endblock %}

##Customizing your application’s templates¶

Astute readers will ask: But if DIRS was empty by default, how was Django finding the default admin templates? The answer is that, since APP_DIRS is set to True, Django automatically looks for a templates/ subdirectory within each application package, for use as a fallback (don’t forget that django.contrib.admin is an application).

Our poll application is not very complex and doesn’t need custom admin templates. But if it grew more sophisticated and required modification of Django’s standard admin templates for some of its functionality, it would be more sensible to modify the application’s templates, rather than those in the project. That way, you could include the polls application in any new project and be assured that it would find the custom templates it needed.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值