像下一个示例一样,如何在django admin中为表单字段添加提示?
(此处:URL和内容描述在字段下以灰色显示)
Answers:
在models.py中定义字段时:
myfield = models.CharField(max_length=100, help_text="This is the grey text")
收藏链接:
https://docs.djangoproject.com/en/dev/ref/models/fields/#help-text
我发现自己一直在引用它(不仅是对于help_text,而且还涉及与模型字段有关的一切)!
<br/>
换行符
除了邓肯的回答,如果你想显示一些额外的帮助文本,并希望有一些有用的标记过,你可以自定义显示各个形式字段集并添加描述字段。以您的示例为例,您想将“内容”字段细分为它自己的字段集块,并添加一些详细的帮助文本。您可以执行以下操作:
from mymodel.models import MyModel
from django.contrib import admin
"""
Custom Help Text
"""
CONTENT_HELP_TEXT = ' '.join(['<p>Here is some multi-line help',
'which is a long string so put',
'into a list which is then joined',
'with spaces. I can do fun things',
'like have <strong>bold</strong>',
'and some line breaks.<br/>'])
"""
Customize Admin
"""
class MyModelAdmin(admin.ModelAdmin):
"""
Add your other customizations
like actions, list_display, list filter, etc
"""
fieldsets = [
('Content', {
'fields':('content',),
'description': '<div class="help">%s</div>' % CONTENT_HELP_TEXT,
}),
]
admin.site.register(MyModel, MyModelAdmin)
有关更多信息,请参见Django文档(向下滚动至字段集)区域。
models.FooField
而不是forms.FooField
。应该是docs.djangoproject.com/en/dev/ref/models/fields/#help-text