I have a model:
models.py
class Doc(models.Model):
series = models.CharField(max_length=50, help_text="2", blank=True, null=True, default=None)
number = models.CharField(max_length=50, help_text="2", blank=True, null=True, default=None)
name = models.CharField(max_length=150, help_text="3")
citizenship = models.ManyToManyField(Citizenship, help_text="4")
forms.py
class DocForm(ModelForm):
class Meta:
model = Doc
fields = '__all__'
How do i add 2 legends for these fields? 1 for series and number and 1 for name and citizenship?
template
{% for field in form %}
<div class="form-group">
<label for="{{ field.id_for_label }}" class="control-label col-md-3">{{ field.label }}
{% if field.field.required %}<span class="required"> * </span> {% endif %}
</label>
<div class="col-md-4">
{{ field }}
{% if field.errors %}
{% for error in field.errors %}
<span id="{{ field.id_for_label }}-error" class="help-block help-block-error">{{ error }}</span>
{% endfor %}
{% endif %}
</div>
</div>
{% endfor %}
答案 0 :(得分:2)
我正在写这个,也许其他人也会需要它:
这就是我的成就:
<强> forms.py 强>
class DocForm(BetterModelForm):
class Meta:
model = Doc
fields = '__all__'
class Meta:
model = Doc
fieldsets = [
('main',
{'fields': ['name', 'citizenship'],
'legend': 'I. PERSONAL DATA'}),
('main1',
{'fields': ['series', 'number'],
'legend': 'II. PROFESSIONAL IDENTIFICATION'})]
模板
{% for fieldset in form.fieldsets %}
<fieldset class="{{ fieldset.classes }}">
{% if fieldset.legend %}
<legend>{{ fieldset.legend }}</legend>
{% endif %}
{% for field in fieldset %}
<div class="form-group">
<label for="{{ field.id_for_label }}" class="control-label col-md-3">{{ field.label }}
{% if field.field.required %}<span class="required"> * </span> {% endif %}
</label>
<div class="col-md-4">
{{ field }}
{% if field.errors %}
{% for error in field.errors %}
<span id="{{ field.id_for_label }}-error" class="help-block help-block-error">{{ error }}</span>
{% endfor %}
{% endif %}
</div>
</div>
{% endfor %}
</fieldset>
{% endfor %}