【发布时间】: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