Python 我可以在 django 模型字段中添加帮助文本吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18178310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:06:18  来源:igfitidea点击:

Can i add help text in django model fields

pythondjangodjango-models

提问by user26

I have a Student detail page where I have all the student data shown in a log nested format.

我有一个学生详细信息页面,其中以日志嵌套格式显示了所有学生数据。

Now in the form I know I can add a help text. But now my manager wants that when we show the detail page, there should be help on hovering over each field.

现在在我知道的表单中,我可以添加帮助文本。但是现在我的经理希望当我们显示详细信息页面时,应该有助于将鼠标悬停在每个字段上。

Now I am confused where should I enter 50-100 words help text for each table in 5-6 tables

现在我很困惑我应该在哪里为 5-6 个表格中的每个表格输入 50-100 个字的帮助文本

This is how I used help_text in the forms:

这就是我在表单中使用 help_text 的方式:

student_number = forms.CharField(
        required=False, max_length=64, label='Student Number',
        help_text='Unique identifier for the student ')

采纳答案by nim4n

Yes you can! Just like your form, you can add help_textto your model fields.

是的你可以!就像您的表单一样,您可以添加help_text到模型字段中。

回答by djangonaut

By 'detail page' you mean a edit form of a single student instance or the list of all student records? Are you using Django admin or do you use your own view and template, custom form definition or as_ul()/as_list() etc? It's hard to answer your question with just seeing your form field definition.

“详细信息页面”是指单个学生实例的编辑表单还是所有学生记录的列表?您是使用 Django admin 还是使用自己的视图和模板、自定义表单定义或 as_ul()/as_list() 等?仅查看表单字段定义很难回答您的问题。

What do you mean by 'for each table'? Would form inheritancehelp, so that you set the help text of common form fields only in the super form.

“每张桌子”是什么意思?将表单继承帮助,让您只在超级表单中设置常见表单字段的帮助文本。

If you render a custom template you can render the help_text wherever you like with {{ my_field.help_text }}. If you have a table-like view in your template and want the helptext there, just put an empty instance of the form in your template context so that you have access to the help_texts and put it in your table as tooltip?

如果您渲染自定义模板,您可以使用 {{ my_field.help_text }} 在您喜欢的任何地方渲染 help_text。如果您的模板中有一个类似表格的视图并且想要帮助文本,只需在模板上下文中放置一个空的表单实例,以便您可以访问 help_texts 并将其作为工具提示放入表格中?

回答by ecp

In case you want to use the standard admin change form with a short help-text for each field but sometimes feel the need to give a longer explanation or even a table with some sample values (without restricting the user to a predefined set of choices) you can do something like this:

如果您想使用带有每个字段的简短帮助文本的标准管理更改表单,但有时觉得需要给出更长的解释,甚至需要提供一些示例值的表格(而不将用户限制为一组预定义的选项)你可以这样做:

my_strange_field = models.FloatField('strange parameter', validators=[MinValueValidator(1.0)],
                                     help_text='corr. factor x >= 1 <img src="/static/admin/img/icon-unknown.gif" '
                                               'width="15" height="15" title="typical values:\n'
                                               'cow:\t2..3\ncat:\t5..7\ndog:\t11..15')

This way you get a short text "corr. factor x >= 1" followed by a nifty question mark which presents a table like tool-tip without the need to modify the change_form template.

通过这种方式,您会得到一个简短的文本“corr. factor x >= 1”,后跟一个漂亮的问号,它显示了一个类似于工具提示的表格,而无需修改 change_form 模板。

回答by tomcounsell

When using model forms you can add labels and help_texts to fields generated by the model. see docs

使用模型表单时,您可以向模型生成的字段添加标签和 help_texts。查看文档

class PupilForm(forms.ModelForm):

  class Meta:
    model = Pupil

    fields = ['student_number',]
    labels = {'student_number': "Student Number",}
    help_texts = {'student_number': "Unique identifier for the student",}

回答by Radren

After add help_texts in form, you should do smth like this in a frontend:

在表单中添加 help_texts 后,您应该在前端执行以下操作:

<label title="{{ form.name.help_text }}" for="{{ form.name.id_for_label }}">Your label</label> 
{{ form.name }} {{ form.quantity }}