我正在学习 python/Django 并建立我的第一个项目。一切都很顺利,但我一直在疯狂地寻找一些非常简单的东西。
有一个默认菜单项“身份验证和授权”,我想更改名称。我在模板中搜索了是否需要扩展某些内容,我搜索了是否有
.po
文件或其他文件,但我找不到它,也没有提示我应该在 admin.py
中覆盖哪个参数来设置它。
我并不是想安装多语言或某些高级本地化,只是想更改该菜单项的名称:)
有什么想法吗?
跟进 Lorenzo 的回答(Django 3.0):
在项目中的某个位置使用自定义 AppConfig 创建文件
# 'authconfig.py'
from django.apps import AppConfig
class CustomAuthConfig(AppConfig):
name = 'django.contrib.auth'
verbose_name = 'my super special auth name'
然后,在settings.py中:
INSTALLED_APPS = [
...
#'django.contrib.auth', # comment out to avoid duplicate apps
'authconfig.CustomAuthConfig', # add this in for custom config of auth module
...
]
创建指向原始
AppConfig
模块的自定义 django.auth
并覆盖 verbose_name
。然后在 AppConfig
中使用您的自定义 INSTALLED_APPS
而不是原始 auth
应用程序。
https://docs.djangoproject.com/en/1.10/ref/applications/#configuring-applications
如果您使用自定义语言环境(请参阅如何覆盖 django 管理翻译?),那么最好的方法是将这些行添加到 django.po 文件中:
msgid "Authentication and Authorization"
msgstr "Your text for the auth app"
由于某种原因,许多本地化文件未能包含“身份验证和授权”字符串的翻译。像这样添加它(并使用上面链接中提到的compilemessages命令)应该可以正常工作。
最简单的方法是覆盖任何应用程序中的
verbose_name
。只需将此行添加到 django.contrib.auth
之后注册的任何应用程序中即可
from django.contrib.auth.apps import AuthConfig
AuthConfig.verbose_name = "Your favourite name"
这至少不能通过模板干净地完成..
您可以将身份验证应用程序详细名称“身份验证和授权”放在您自己的 .po 文件中(并遵循 Django 文档的翻译) 这样 Django 通常会使用你的名字。