关于字段的 verbose_name
有一个类似的问题:How to stop auto-capitalization of verbose_name in django
当在管理索引页面中列出应用程序的可用模型时,Django 始终将模型的 verbose_name_plural
的首字母大写并将其用作模型名称。
这是来自 django.contrib.admin.sites.py 的代码:
model_dict = {
'name': capfirst(model._meta.verbose_name_plural),
'perms': perms,
}
但请考虑以下 screenshot ,我想显示“vCenters”而不是“VCenters”。
我可以删除 capfirst
,并显式地将其他模型的 verbose_name_plural
大写以使其工作。
但是我得改django的源码,看来不是django的bug。有没有更好的解决方案?
最佳答案
没那么容易...
- 将
admin/index.html
模板复制到您的template/admin/index.html
- 创建您自己的模板过滤器:
lowerfirst_if_starts_with_v
在您的 拥有templatetags/my_special_thing.py
目录
@register.filter(is_safe=True)
@stringfilter
def lowerfirst_if_starts_with_v(value):
"""Lowercase the first character of the value."""
return value and value[0] =='v' and value[0].lower() + value[1:]
- 将其加载到
index.html
{%load my_special_thing%}
- 将其应用于
23
行的index.html
<th scope="row"><a href="{{ model.admin_url }}"> \
{{ model.name|lowerfirst_if_starts_with_v }}</a></th>
完成。
关于python - Django 总是在管理索引页面中将模型的详细名称大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12316259/