3 ms·
How well does Django handle complex forms? The kind where "if x = y show fields p-r (and make them required), if x = z jump the next screen)? I've just impleme
by porker 14y ago
How well does Django handle complex forms? The kind where "if x = y show fields p-r (and make them required), if x = z jump the next screen)?
I've just implemented one of these in PHP using the 'old' Symfony 1.4 and it was painful. I ended up hardcoding most of the frontend, and writing ridiculous modifications for "add another row" and validation.
Is Django any better at that? Complex forms have been my pain point for over 11 years, and if a framework handles them... I'm in!
- ShaunCodeweaver 14y agoVery much this - though I've experienced this in other stacks. Would love to know the best way people handle this.
- scott_w 14y agoIf I'm understanding you correctly, you want to have the fields on the form depend on the contents of what was entered on the previous form e.g. if user selects name, ask for name, otherwise ask for address. In that case, I tend to use class-based views and set different forms, templates etc. depending on those values. If the form is different enough, it's easier just to send them to a completely different URL.
- porker 14y agoNot quite - that happens sometimes and I use logic in the controller (ugh) to switch the form class. This is what I mean: an adaptive form http://screencast.com/t/SutggDU5 http://screencast.com/t/SutggDU5 The server-side validation has to adapt depending on the data coming from the client-side, and varies a lot. Perhaps others don't build forms like this much and so web frameworks don't deal with them; I get one like this every few months!
- scott_w 14y agoAh, I see now. For certain cases e.g. repeating the same form over and over, you can use formsets, but that doesn't work when you have two different forms. I still think a solution is to use multiple forms and form types rather than one big form. Perhaps you can prefix the field names on the template and use that to determine which form they go to inside the view. It makes your logic a little simpler in any case.
- masklinn 14y agoNote that one thing you can do is build smaller forms and mix them up. There's no issue with mixing up 3 or 4 smaller (django) forms in the same (HTML) form, quite the opposite.
- TomaszZielinski 13y agoRe server-side validation that adapts to the incoming data, I think it's easier to write a small mixin that fits your needs than to use a generic library (assuming there's one).
- vially 14y agoI've recently (2-3 months) switched to Python/Django and my experience so far has been very positive. Multi-step forms can be implemented in Django using Form wizards [1]. In order to optionally show fields or make them required I think you could build it easily using the WizardView.get_form method [2]. The functionality to skip steps is built-in and it's very easy implement using conditional views [3]. In conclusion, it's fairly easy to implement the functionality you described using only the core framework without the need to rely on external packages. [1] - https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/ https://docs.djangoproject.com/en/dev/ref/contrib/formtools/... [2] - https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/#django.contrib.formtools.wizard.views.WizardView.get_form https://docs.djangoproject.com/en/dev/ref/contrib/formtools/... [3] - https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/#conditionally-view-skip-specific-steps https://docs.djangoproject.com/en/dev/ref/contrib/formtools/...
- porker 14y agoInteresting, thanks for sharing your experience! Have you seen anything saying it helps to build adaptive forms like [1]? [1] - http://screencast.com/t/SutggDU5 http://screencast.com/t/SutggDU5
- vially 14y agoI'm not aware of any library which could help you build these kind of adaptive forms automatically. However, the way I would go about building something like that is by using the form wizard (mentioned in my previous comment) with a combination of forms/modelforms [1] and formsets [2]. Formsets are used in situations when you need to display the same form multiple times on the same page (like the multiple addresses/benefits income in your example). However, you would have to implement all the client side interaction yourself since Django does not provide this by default (there are some libraries which are supposed to help with this but I haven't tried them myself [3]). One thing worth mentioning is that by default, the form wizard only allows you to display either a Form, or a Formset in a step/page. However, there is a ticket open for this [4] which links to a helper class: form_container.py [5] which helps overcome this problem. Hopefully, this will be included in future versions of Django. Hope this helps. [1] - https://docs.djangoproject.com/en/dev/topics/forms/modelforms/ https://docs.djangoproject.com/en/dev/topics/forms/modelform... [2] - https://docs.djangoproject.com/en/dev/topics/forms/formsets/ https://docs.djangoproject.com/en/dev/topics/forms/formsets/ [3] - https://code.google.com/p/django-dynamic-formset/ https://code.google.com/p/django-dynamic-formset/ [4] - https://code.djangoproject.com/ticket/18830 https://code.djangoproject.com/ticket/18830 [5] - https://code.djangoproject.com/attachment/ticket/18830/form_container.py https://code.djangoproject.com/attachment/ticket/18830/form_...
- TomaszZielinski 13y agoI would say that it's not much better out of the box but you can combine forms, formsets, custom validation, 3rd party helpers like django-cripsy-forms and support with something like KnockoutJS in the browser and voila :) It would be great if there was a ready-made add-on combining all these areas but I don't know any personally. I also suspect think that writing such a generic helper might not be a great idea as everyone needs something a bit different, so using the mentioned lower level components and combining them on your own could be the best way to go here.