【问题标题】:How to change help text of a django form field?如何更改 django 表单字段的帮助文本?
【发布时间】:2014-08-12 06:10:11
【问题描述】:

这是我的群组消息模型,它扩展了另一个模型 Message,它有一些其他字段,如 texttime 等。

class GroupMessage(Message):
    group = models.ForeignKey(Group, related_name='+')

以下是我为此模型创建的表格。

class GroupForm(ModelForm):
    class Meta:
        model = GroupMessage

如何更改表单中组字段的帮助文本?任何帮助将不胜感激。

【问题讨论】:

  • 你是说标签吗?这是类似的问题:stackoverflow.com/questions/14027185/… - 但是,它没有正确回答!
  • 我从来没有这样做过.. 但在 fields.py 中我看到类似help_text.. 然后尝试一下..help_text='Your help message'。顺便提一句。你不应该使用_ 作为类名。 _ 用于受保护的方法/变量。

标签: django django-forms


【解决方案1】:

对于 Django >= 1.6 (docs):

class GroupForm(ModelForm):
    class Meta:
        model = GroupMessage
        help_texts = {
            'group': 'Group to which this message belongs to',
        }

【讨论】:

    【解决方案2】:

    我用下面的代码让它工作了。

    class _GroupMessageForm(ModelForm):
    
        class Meta:
            model = GroupMessage
    
    
    class GroupMessageForm(_GroupMessageForm):
    
        def __init__(self, *args, **kwargs):
            super(_GroupMessageForm, self).__init__(*args, **kwargs)
            self.fields['employees'].help_text = '<br/>Hold down "Control" to select more.'
    

    【讨论】:

    • 为了将来参考,这包含在文档中:Field Options
    • 我不会在表单中使用 help_text,而是将其添加到模型中字段的定义中。这样,我们使用该字段的形式并不重要。 group = models.ForeignKey(Group, related_name='+', help_text='&lt;br /&gt;Hold down "Control to select more.")
    【解决方案3】:

    有多种方法可以做到这一点。

    1)您可以将 help_text 添加到模型中的字段定义中,正如Bobort 在上面的评论中所说:

    class GroupMessage(Message):
        group = models.ForeignKey(Group, related_name='+', help_text='Some text')
    

    如果您希望此 help_text 保持不变,无论您使用什么 ModelForm,这种方式都非常有用。例如,您可以为 GroupMessage 模型创建两个 ModelForm 表单,它们都将具有来自该模型的 help_text。

    2)您可以像这样覆盖表单字段中的模型字段:

    class GroupForm(ModelForm):
        group = forms.ModelChoseField(label='Group', help_text='Some text')
    
        class Meta:
            model = GroupMessage
    

    当您不仅需要更改 help_text 还需要更改标签或查询集或字段类型时,这种方式很有用。

    3) 你可以像上面的laffuste 那样做:

    class GroupForm(ModelForm):
        class Meta:
            model = GroupMessage
            help_texts = {
                'group': 'Group to which this message belongs to',
            }
    

    如果您只想更改一个或多个字段的 help_text,这种方法很有用。

    4) 另一种方法就像你做的那样:

    class GroupForm(ModelForm):
        class Meta:
            model = GroupMessage
    
        def __init__(self, *args, **kwargs):
            super(GroupForm, self).__init__(*args, **kwargs)
            self.fields['employees'].help_text = 'Some text'
    

    但是这个解决方案需要一点说明。如果您将在这样的模板中使用此表单:

    {% for field in form %}
        {{ field }}
        {{ field.help_text }}
    {% endfor %}
    

    没关系。但例如以防万一:

    {% for field in form.visible_fields %}
        {{ field }}
        {{ field.help_text }}
    {% endfor %}
    

    help_text 将为空,因为 BoundField 中的 help_text 在您自己设置之前已填充。因此解决方案是将self['employees'].help_text = 'Some text' 添加到__init__ 或在模板中使用{{ field.field.help_text }}

    当您想通过某些条件设置 help_text 时,此解决方案很有用,例如,如果将特定参数传递给表单初始化或其他内容。

    希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-05-29
      • 1970-01-01
      • 2018-11-22
      • 2015-08-03
      • 2021-10-27
      • 2022-10-25
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多