如何在Django Admin中的用户变更页面添加描述?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Django Admin中的用户变更页面添加描述?相关的知识,希望对你有一定的参考价值。
我正在使用Django,想在用户变更页面红色标记处的标题下添加一个描述,如图所示。
我的admin.py文件是这样的。
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
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')}),
)
fieldsets = (
('Personal Information', {
'classes': ('wide',),
'fields': (('first_name','last_name'),'email','a1','a2','city','state','pincode','password')}),
('Permissions', {'fields': ('is_superuser','is_staff', 'is_active','date_joined')}),
)
那么我应该怎么做才能在用户创建页面的红色标记处得到一个描述呢?
先谢谢你了!
答案
要在用户变更页中添加描述,首先你要做的就是在你的admin文件中添加一行,那就是 。
change_form_template = 'change_form.html'
这样,你的整个管理文件看起来就像 。
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
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')}),
)
fieldsets = (
('Personal Information', {
'classes': ('wide',),
'fields': (('first_name','last_name'),'email','a1','a2','city','state','pincode','password')}),
('Permissions', {'fields': ('is_superuser','is_staff', 'is_active','date_joined')}),
)
之后,去模板文件夹,并添加一个html文件的名称 。change_form.html
然后在其中添加以下代码 change_form.html
:
{% extends "admin/change_form.html" %}
{% load i18n admin_urls static admin_modify %}
{% block form_top %}
<p>Add here your description for User Change Page.</p>
{% endblock %}
这就是你要做的一切。
以上是关于如何在Django Admin中的用户变更页面添加描述?的主要内容,如果未能解决你的问题,请参考以下文章
Django Admin Cookbook-39如何两次向Django管理员添加模型
Django-admin:如何在记录更改列表中显示指向对象信息页面的链接而不是编辑表单?