我正在Django上工作,想要更改Django管理的change list页标签的默认标题,如图所示:
我的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')}),
)
那么有没有办法可以改变它呢?
提前感谢!!
发布于 2020-04-24 09:00:59
是的,有一个方法可以做到这一点。
您需要做的是将以下函数添加到您的管理文件中:
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)
这就是你要做的全部。
https://stackoverflow.com/questions/61404511
复制相似问题