如何更改Django admin更改列表页选项卡的标题

2024-04-25 20:48:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理Django,希望更改Django admin的更改列表页选项卡的默认标题,如图中所示:

enter image description here

我的admin.py文件是:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin


from .models import CustomUser


class CustomUserAdmin(UserAdmin):

    change_list_template='change_list_form.html'

    change_form_template = 'change_form.html'

    add_form_template='add_form.html'

    list_display = ('first_name','last_name','email','is_staff', 'is_active',)
    list_filter = ('first_name','email', 'is_staff', 'is_active',)

    search_fields = ('email','first_name','last_name','a1','a2','city','state','pincode')
    ordering = ('first_name',)

    add_fieldsets = (
        ('Personal Information', {
            # To create a section with name 'Personal Information' with mentioned fields
            'description': "",
            'classes': ('wide',),  # To make char fields and text fields of a specific size
            'fields': (('first_name','last_name'),'email','a1','a2','city','state','pincode','check',
                       'password1', 'password2',)}
        ),
        ('Permissions',{
            'description': "",
            'classes': ('wide', 'collapse'),
            'fields':( 'is_staff', 'is_active','date_joined')}),
    )

那么有什么办法可以改变它吗

提前谢谢


Tags: namefromimportformaddfieldsadminis
1条回答
网友
1楼 · 发布于 2024-04-25 20:48:08

是的,有办法做到这一点

您需要做的是将以下功能添加到您的管理文件中:

    def changelist_view(self, request, extra_context=None):
        extra_context = {'title': 'Type here new title for your change list page tab'}
        return super(CustomUserAdmin, self).changelist_view(request, extra_context=extra_context)

因此,您的管理文件将如下所示:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin


from .models import CustomUser


class CustomUserAdmin(UserAdmin):

    change_list_template='change_list_form.html'

    change_form_template = 'change_form.html'

    add_form_template='add_form.html'

    list_display = ('first_name','last_name','email','is_staff', 'is_active',)
    list_filter = ('first_name','email', 'is_staff', 'is_active',)

    search_fields = ('email','first_name','last_name','a1','a2','city','state','pincode')
    ordering = ('first_name',)

    add_fieldsets = (
        ('Personal Information', {
            # To create a section with name 'Personal Information' with mentioned fields
            'description': "",
            'classes': ('wide',),  # To make char fields and text fields of a specific size
            'fields': (('first_name','last_name'),'email','a1','a2','city','state','pincode','check',
                       'password1', 'password2',)}
        ),
        ('Permissions',{
            'description': "",
            'classes': ('wide', 'collapse'),
            'fields':( 'is_staff', 'is_active','date_joined')}),
    )

    def changelist_view(self, request, extra_context=None):


        extra_context = {'title': 'Type here new title for your change list page tab'}


        return super(CustomUserAdmin, self).changelist_view(request, extra_context=extra_context)

这就是你要做的

相关问题 更多 >