django定制后台
📅 2026/7/28 16:13:42
👁️ 阅读次数
📝 编程学习
后台数据库显示
对应app下的admin.py
from django.contrib import admin from article_app.models import Article # 引入数据模型引入APP的文章数据库,但是要具体定制就不用在这引入
#admin.site.register(Article)后台登录进去的标题
admin.site.site_header = '三门峡市统计信息局网站后台管理'修改后台登录的文字
admin.site.site_title = '三门峡市统计信息局'声明你想定制的表
@admin.register(Article) class ArticleAdmin(admin.ModelAdmin): # 设置要显示在列表中的字段(id字段是Django模型的默认主键) list_display = ('title', 'author', 'time', 'views', 'source', 'sort') # 设置每页显示多少条记录,默认是100条 list_per_page = 12 # 设置默认排序字段,负号表示降序排序 # ordering = ('-name',) # 操作项功能显示位置设置,两个都为True则顶部和底部都显示 actions_on_top =True actions_on_bottom = True # 操作项功能显示选中项的数目 actions_selection_counter = True # 字段为空值显示的内容 empty_value_display = ' -无字段- ' # 设置默认可编辑字段(name默认不可编辑,因为它是一个链接,点击会进入修改页面) # list_editable = ['TEL', 'member_type',] # fk_fields 设置显示外键字段 # fk_fields = ('member_type',) # 过滤器功能及能过滤的字段 list_filter = ('title', 'author') # 搜索功能及能实现搜索的字段 search_fields = ('id', 'title', 'author', 'sort') 后台app名字显示 #对应APP下的__init__ from django.apps import AppConfig import os#修改对应的‘article_app.ArticleAppConfig’这个名字与apps.py下的函数名字保持一致
与下面的类的名字也是一致
default_app_config = 'article_app.ArticleAppConfig' def get_current_app_name(_file): return os.path.split(os.path.dirname(_file))[-1] class ArticleAppConfig(AppConfig): name = get_current_app_name(__file__) verbose_name = "文章管理"后台显示表名
class Meta: db_table = 'Opinion' # 存进数据库里的表名 verbose_name_plural = '意见' # 后台显示的表名
编程学习
技术分享
实战经验