# Auxiliar functions def normalize(cadena): return ''.join([x for x in normalize('NFD', cadena) if category(x) != 'Mn']) def to_normal_text(cadena): return normalize(cadena.lower().strip().replace(' ', '_').replace('/', '_')) # Snippet functions def add_form_field(form_map, form_list, name, form_type, post, initial=None): if not initial: initial = {} initial.update({'label': name}) form_id = to_normal_text(name) form = form_type(post, initial=initial, prefix=form_id) form_map[form_id] = form form_list.append(form) def form_map_is_valid(form_map): valid = True for form in form_map: if form_map[form]: valid = valid and form_map[form].is_valid() return valid # Snippet: form.py class MainVariableForm(forms.Form): #add this form for the csrf in case the form would be a post def __init__(self, *args, **kwargs): super(FormCompuestoPrincipal, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_tag = False class FieldForm(forms.Form): def get_filter(self): if self.cleaned_data['field']: return {'key': self.fields['field'].label, 'value': self.cleaned_data['field']} return None def __init__(self, *args, **kwargs): super(FieldForm, self).__init__(*args, **kwargs) self.fields['field'].label = kwargs.get('initial').get('label') self.helper = FormHelper() self.helper.form_tag = False self.helper.disable_csrf = True # Examples: forms.py class SpecificFieldForm1(FieldForm): field = forms.ChoiceField(choices=[('choice_1', '1'), ('choice_2', '2')) def get_filter(self): if self.cleaned_data['field']: return {'key': self.fields['field'].label, 'value': dict(self.fields['field'].choices)[self.cleaned_data['field']]} return None class SpecificFieldForm2(FieldForm): field = forms.CharField(required=False) # Example: view.py def some_view(request): form_list = [] form_map = {} add_form_field(form_map, form_list, 'Field 1 Label', SpecificFieldForm1, request.GET) add_form_field(form_map, form_list, 'Field 2 Label', SpecificFieldForm2, request.GET) add_form_field(form_map, form_list, 'Field 3 Label', SpecificFieldForm1, request.GET) filters= None if form_map_is_valid(form_map): # do stuff, use form_map['field_1_label'].cleaned_data['field'] for field reference. filters= [form.get_filter() for form in form_list] template = loader.get_template('some_template.html') context = RequestContext(request, { 'forms': form_list, 'filtros': filtros, }) return HttpResponse(template.render(context)) # Example: some_template.html