I have been trying to learn Django (and Python). I have a form that can have up to n elements. Each of those elements has 3 parts to it (a TextField, a TagField and a Checkbox). The form init looks like this
def __init__(self, *args, **kwargs):
notesAndTags = kwargs.pop('notesAndTags')
super(NotesAndTagsForm, self).__init__(*args, **kwargs)
for i, tagandtext in enumerate(notesAndTags):
self.fields['tag_%s' % i] = TagField(initial = tagandtext.tag)
self.fields['text_%s' % i] = forms.CharField(widget=forms.Textarea, initial=tagandtext.text)
self.fields['import_%s' % i] = forms.BooleanField(initial=True)
This is based on this explanation of dynamic forms the form creates just fine.
The question is how do you then display that form using the correct tags?
If I do a
{{ form }}
in the template how do I ensure that the fields are grouped together as they should be? If I don't use the form that way how do I manually loop through
{% for import_note in form.importNote %}
<div class="field">
<label for="id_field">Tags:</label>
{{ form.fields['tag_%s' % forloop.counter0] }}
</div>
{% empty %}
No data to import
{% endfor %}
gives an error because I tried to use the forloop.counter0. Is there any way to do this without creating a custom form tag?
Thanks