News for hackers. Stuff that matters.
Re:Eighteen months of Django by porker on Wednesday May 08, 2013 @03:26AM (#5672975)
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!

Re:Eighteen months of Django by vially on Wednesday May 08, 2013 @04:35AM (#5673156)
I'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/... [2] - https://docs.djangoproject.com/en/dev/ref/contrib/formtools/... [3] - https://docs.djangoproject.com/en/dev/ref/contrib/formtools/...

Re:Eighteen months of Django by porker on Wednesday May 08, 2013 @05:05AM (#5673238)
Interesting, thanks for sharing your experience!

Have you seen anything saying it helps to build adaptive forms like [1]?

[1] - http://screencast.com/t/SutggDU5

Re:Eighteen months of Django by vially on Wednesday May 08, 2013 @06:02AM (#5673364)
I'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/modelform... [2] - https://docs.djangoproject.com/en/dev/topics/forms/formsets/ [3] - https://code.google.com/p/django-dynamic-formset/ [4] - https://code.djangoproject.com/ticket/18830 [5] - https://code.djangoproject.com/attachment/ticket/18830/form_...

Re:Eighteen months of Django by TomaszZielinski on Monday May 13, 2013 @04:33AM (#5697708)
I 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.

Re:Eighteen months of Django by scott_w on Wednesday May 08, 2013 @04:23AM (#5673121)
If 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.

Re:Eighteen months of Django by porker on Wednesday May 08, 2013 @05:04AM (#5673233)
Not 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

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!

Re:Eighteen months of Django by scott_w on Wednesday May 08, 2013 @05:42AM (#5673315)
Ah, 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.

Re:Eighteen months of Django by masklinn on Wednesday May 08, 2013 @06:09AM (#5673379)
Note 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.
Re:Eighteen months of Django by TomaszZielinski on Monday May 13, 2013 @04:38AM (#5697723)
Re 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).
Re:Eighteen months of Django by ShaunCodeweaver on Wednesday May 08, 2013 @03:44AM (#5673029)
Very much this - though I've experienced this in other stacks.

Would love to know the best way people handle this.