gpt4 book ai didi

python - Django 将字段集图例添加到 ModelForm

转载 作者:行者123 更新时间:2023-12-01 02:33:20 25 4
gpt4 key购买 nike

我有一个模型:

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__'

如何为这些字段添加 2 个图例?1 代表系列和编号,1 代表姓名和公民身份?

模板

{% 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 %}

最佳答案

我正在写这个,也许其他人也需要它:

这就是我的制作方法:

forms.py

class DocForm(BetterModelForm):

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 %}

关于python - Django 将字段集图例添加到 ModelForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46540873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com