python - 更改 Django 管理更改列表标题文本

标签 python python-3.x django django-admin title

我正在为 Django 管理界面创建一些自定义 View ,这些 View 使用标准更改列表作为临时阶段。除了更改列表页面 H1 是“选择要更改的对象”这一事实之外,这工作得很好。对于用户将在我的自定义 View 中执行的操作,“更改”不是正确的动词。

我找到了控制更改列表页面布局的 django.contrib.admin 模板(change_list.htmlchange_list_results.html),但我找不到标题是从哪里提供的。我猜它是某个地方的 View 传入的变量?

如何用较少误导性的内容覆盖此文本,例如“选择对象”而不是“选择要更改的对象”?我可以在所有更改 ListView 中更改它,而不仅仅是我尝试自定义的特定 View ;但如果可能的话,我更喜欢一个覆盖的解决方案,而不是修改 django.contrib.admin 代码。

更新:我找到了负责更改列表的 View ,它是 django\contrib\admin\views 中的 main.py。该变量是第 69 行的 self.title (Django 1.0)。我通过编辑此行获得了我正在寻找的结果

self.title = (self.is_popup and ugettext('Select %s') % force_unicode(self.opts.verbose_name) or ugettext('Select %s to change') % force_unicode(self.opts.verbose_name))

阅读

self.title = (self.is_popup and ugettext('Select %s') % force_unicode(self.opts.verbose_name) or ugettext('Select %s') % force_unicode(self.opts.verbose_name))

我仍然非常有兴趣听到一种更好的方法来实现相同的结果,而不涉及破解 django.contrib.admin 代码 - 看起来已经有一个选项可以按照我的方式获得标题我喜欢它,但我不知道如何触发它?

最佳答案

不确定是否仍然相关,但另一种方法是为 changelist_view 方法传递 extra_content 。例如:

from django.contrib import admin

class MyCustomAdmin(admin.ModelAdmin):

    def changelist_view(self, request, extra_context=None):
        extra_context = {'title': 'Change this for a custom title.'}
        return super(MyCustomAdmin, self).changelist_view(request, extra_context=extra_context)

关于python - 更改 Django 管理更改列表标题文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1310974/

相关文章:

Python 3.7 字典给出错误的值作为输出

python - Azure Linux Web 应用程序(startup.sh : not found)

python - 将字符串变量读入变量

python - 如果与 matplotlib.pyplot 一起使用,Tkinter 窗口不会关闭

python - 如何使用 EmailMessage (Django) 在消息中插入新行

python - 遍历列表时如何删除元素

python - 第二大行多个 Pandas 列

python - BASH 和 Python

django - Python3导入错误: cannot import name 'cookies' from 'http'

django - 如何预览包含不同语言翻译字段的 Wagtail 页面?