Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex form widgets in Django

I am looking for good helper libraries to generate a rather complex form in Django.

  • Dynamic field dependencies: Say if option a is selected certain fields are shown/hidden and subset of these are mandatory depending on option selection.

  • Add more: On clicking "Add more" button that clones some widget.

This is something which ToscaWidgets is capable of handle.

  • http://toscawidgets.org/documentation/tw.dynforms/tutorial.html#growing

  • Some working ToscaWidgets Demos

Currently I am managing this with some jquery code however not completely satisfied. And sadly cant use ToscaWidgets for some reason.

TIA

like image 836
Shekhar Avatar asked Apr 22 '10 09:04

Shekhar


People also ask

What are widgets in Django forms?

A widget is Django's representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. The HTML generated by the built-in widgets uses HTML5 syntax, targeting <!


1 Answers

I've used a heavily customized Formset to make a rather complicated form with complicated user permission dependencies. This involved subclassing django.forms.formsets.BaseFormSet, overriding __init__, add_fields, is_valid, and save, and of course using a customized Form. The end-product was a bit hairy, but I'm still able to understand and modify it after 6 months or so.

I didn't make use of the Formset ability to work with adding and subtracting subforms, but this should meet your needs.

Documentation at: http://docs.djangoproject.com/en/1.1/topics/forms/formsets/

like image 151
David Eyk Avatar answered Sep 26 '22 02:09

David Eyk