Hi,
Based on http://jacobian.org/writing/dynamic-form-generation I have now
managed to create a data driven form (hooray :-)
Basically I'm reading field names from a database and append to a ModelForm
in its __init__ method:
View:
equipments =
equipment.objects.filter(category__in=request_extra_cat).order_by('category__name','name')
for equip in equipments:
fields.append(equip)
form = AddRequestForm(request.POST or None, extra=fields) # pass extra
fields
Form:
def __init__(self, *args, **kwargs):
extra = kwargs.pop('extra') # Get extra fields
super(AddRequestForm, self).__init__(*args, **kwargs)
for i, question in enumerate(extra):
if 'notes' in question.name:
self.fields['custom_%s' % question.id] =
forms.CharField(label=question.name, required=False,
widget=forms.Textarea(attrs={'rows':'10', 'class':'input-xxlarge'}))
else:
self.fields['custom_%s' % question.id] =
forms.ChoiceField(label=question.name, choices=REQUEST_NUMBERS,
required=False, widget=forms.Select(attrs={'class':'span1'}))
Because I have groups of fields that belong together, I would like to add a
header to each group of form fields.
The perfect solution would be to use a <fieldset> tag, but it also would be
OK if I could just add a <h4>Header</h4> .
The only problem is, how do I know how I can access the field that contains
the grouping information inside the template.
For html tables I use the following solution with ifchanged:
{% for document in documents %}
{% ifchanged %}<tr class="info"><td colspan="8">{{
document.category.name }}</td></tr>{% endifchanged %}
The problem is that the list of fields (form.visible_fields) does not have
the group information to do this in the form template:
{% for field in form.visible_fields %}
<div class="control-group">
{% ifchanged field.groupID %}<h4>{{ field.category.name
}}</h4>{% endifchanged %} #### THIS DOES NOT WORK
<label class="control-label"
for="{{field.id_for_label}}">{{ field.label }}
{% if field.field.required %}
<span class="text-error">*</span>
{% endif %}
</label>
Any idea how I could achieve what I want?
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.