Форум сайта python.su
как определить в шаблоне, что поле формы ModelForm обязательное?
имею форму
class EditFlatSell(forms.ModelForm):
class Meta:
model = FlatSell
{% for field in form %}
<div class="field">
{% if field.errors %}
<span class="error">
{% for error in field.errors %}
{{ error }}<br/>
{% endfor %}
</span>
{% endif %}
{{ field.label_tag }}
{{field}}
{% if field.help_text %}<span class="help">{{ field.help_text }}</span>{% endif %}
</div>
{% endfor %}
Офлайн
{% if field.field.required %}*{% endif %}
Офлайн
Lolkaда, работает. спасибо.
Код:
{% if field.field.required %}*{% endif %}
Офлайн
Еще вариант: у класса формы задать атрибут required_css_class
class EditFlatSell(forms.ModelForm):
class Meta:
model = FlatSell
required_css_class = 'required_field'
Офлайн
Я давно когда то задавался этим вопросом. Ответ такой:
{% if field.field.required %}*{% endif %} fragment is the relevant addition here. It adds the asterix only if the field is required.
Note that we check field.field.required and not field.required. In the template, ‘field’ is a newforms.forms.BoundField instance, which holds the actual
'Field' instance in its ‘field’ attribute.
Отредактировано (Июнь 2, 2010 11:12:56)
Офлайн