Reputation: 409
I want to render only fields in a Django inline_formset.
This is my template:
<div id="storage">
<h1>Storage</h1>
{% for form in storage_formset %}
<div class="form-group">
<label class="col-sm-2 control-label">{{ form.help_texts }}</label>
<div class="col-sm-10">
{{ form.errors }}
{{ form }}
</div>
</div>
{% endfor %}
</div>
And this is my form:
help = {'field1' : "help1", 'field2' : "help2"}
StorageFormSet = inlineformset_factory(WorkOrder, Storage, min_num=0,
max_num=2, validate_min=True, validate_max=True, extra=1, help_texts=help,
fields=('field1', 'field2'))
This one works, but when it renders, it renders everything (field and field name) in the form and I want it to only render the field. Other thing is that the help_texts
don't work (I don't know if I'm using it right).
If I use:
{{ form.field1 }}
It renders field1, but I want to do it dynamically, so I don't have to repeat again and again.
And if I use:
{{ form.fields }}
It renders a bunch of code
OrderedDict(
[('field1', <django.forms.fields.TypedChoiceField object at 0x7f8f6409c890>),
('field2', <django.forms.fields.IntegerField object at 0x7f8f6409c650>),
(u'id', <django.forms.models.ModelChoiceField object at 0x7f8f6409c1d0>),
(u'DELETE', <django.forms.fields.BooleanField object at 0x7f8f6409cc90>),
('parent_field', <django.forms.models.InlineForeignKeyField object at 0x7f8f659c4e10>)])
I don't know what else to do.
Thank you if you can help me.
Upvotes: 1
Views: 337
Reputation: 6488
I modified your code to only render fields and their associated help texts:
<div id="storage">
<h1>Storage</h1>
{% for form in storage_formset %}
<div class="form-group">
<label class="col-sm-2 control-label">{{ form.help_texts }}</label>
<div class="col-sm-10">
{{ form.errors }}
{% for field in form %}
{{ field }} {{ field.help_text }}
{% endfor %}
</div>
</div>
{% endfor %}
</div>
For more information about accessing form fields in a template visit the docs.
Upvotes: 1