【问题标题】:Django input_formats removes help_text? I want only to change the way one DateField is displayedDjango input_formats 删除了 help_text?我只想更改一个 DateField 的显示方式
【发布时间】:2017-03-20 11:29:24
【问题描述】:

我的问题

我的问题是,在forms.py 中我的AbonnentForm(ModelForm) 上设置input_formats 时,模型的help_text 没有显示在HTML 表单中。

我只想更改一个 DateField 的显示方式。

我做错了什么?

我的代码

这是我的 models.py(缩短) 从 django.db 导入模型

def get_letzte_ausgabe_default():
    return date(date.today().year, 12, 1) if date.today() > date(2017, 12, 26) else date(date.today().year + 1, 12, 1)

class Abonnent(models.Model):
    erste_ausgabe = models.DateField(default=get_letzte_ausgabe_default(),
                                     verbose_name="Erste Ausgabe",
                                     help_text="Eingabe im Format MM/JJJJ. Zum Beispiel: 01/2018)

这是我的 forms.py(完整)

from django.forms import ModelForm, DateField, DateInput
from .models import Abonnent


class AbonnentForm(ModelForm):
    # WHEN I ADD THE FOLLOWING LINE, THE HELP_TEXT IS NOT DISPLAYED UNDER THE HTML-FORM
    erste_ausgabe = DateField(input_formats=['%m %Y'],
                              widget=DateInput(format='%m %Y'))
    class Meta:
        model = Abonnent
        fields = '__all__'

这是我的模板(缩短)

<div class="col-sm {% if form.letzte_ausgabe.errors %} has-danger {% endif %}">
  <label class="col-form-label" for="{{ form.letzte_ausgabe.id_for_label }}">{{ form.letzte_ausgabe.label }}</label>
  <input id="{{ form.letzte_ausgabe.id_for_label }}" class="form-control" type="text" value="{{ form.letzte_ausgabe.value }}" name="{{ form.letzte_ausgabe.html_name }}" placeholder="MM/JJJJ" aria-describedby="{{ form.letzte_ausgabe.id_for_label }}Help">
  {% if form.letzte_ausgabe.help_text %}<small id="{{ form.letzte_ausgabe.id_for_label }}Help" class="form-text text-muted">{{ form.letzte_ausgabe.help_text }}</small>{% endif %}
</div>

【问题讨论】:

    标签: django django-models django-forms


    【解决方案1】:

    这是因为您“覆盖”了ModelForm 内的erste_ausgabe 字段,因此不会显示help_text

    根据docs,您可以在ModelForm 类中重新指定help_text

    class AbonnentForm(ModelForm):
        erste_ausgabe = DateField(input_formats=['%m %Y'],
                                  widget=DateInput(format='%m %Y'),
                                  help_text="Eingabe im Format MM/JJJJ. Zum Beispiel: 01/2018")
    

    【讨论】:

      【解决方案2】:

      您可以尝试在__init__ 方法中更改input_formatswidget

      class AbonnentForm(ModelForm):
          # ...
      
          def __init__(self, *args, **kwargs):
              super(AbonnentForm, self).__init__(*args, **kwargs)
              self.fields['erste_ausgabe'].input_formats=['%m %Y']
              self.fields['erste_ausgabe'].widget=DateInput(format='%m %Y')
      

      这避免了像 Nik 的回答那样在模型表单中复制 help_text

      【讨论】:

      • 哦,是的!这比我的答案要好得多。我的 DRY 原则怎么了 :) ?不过我不会删除它,我还是让它浮在那里吧……
      • 这不起作用:self.fields['erste_ausgabe'].widget=DateInput(format='%m %Y')。我尝试了self.fields['letzte_ausgabe'].widget.format='%m %Y' 之类的替代方法,但它不适用。有什么想法吗?
      • '%m %Y' 是有效的strformat,我看不出有问题。不起作用的是字段值显示为“Dez. 2018 年 1 月 1 日,而不是 2018 年 12 月。我知道这是一个不同的问题,这就是为什么我已经接受了。想一想,你可能知道一个快速的解决方案。
      • __init__ 方法中设置字段的小部件是一种常见的方法。我已经测试了上面的代码,并且设置self.fields['erste_ausgabe'].widget=DateInput(format='%m %Y') 有效。
      • 它对我不起作用,但我省略了默认值,我怀疑这是问题所在。我创建了一个新问题here。感谢您的支持。
      猜你喜欢
      • 1970-01-01
      • 2019-01-20
      • 2012-10-23
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多