Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex forms in Django - what apps and Django/Python features should I look at?

There are a lot of complex forms in my project and I keep getting the feeling that I could be coding them much more elegantly and simply. So my question is what are some good apps and practices that might help me? Specifically, I'm thinking about situations when I need to do stuff like:

  • edit/add more than one object via one form (example: Lets say I have a Partnership model and a Person model - every partnership object is related to two people. Now lets say I want to edit the partnership and the two people in the partnership simultaneously.)
  • deal with many to many relationships - particularly those that have extra data associated
  • "wizard-like" forms (as in there's a couple of pages/steps and the user has to get through all of them before anything gets saved to the database)
  • giving suggestions for what to write into a form field based on what's in the database (I guess this is an AJAX question really, but I'm interested in whether there are some django apps that simplify this somehow)

Solutions to any other more complex form scenarios also welcome. The above are problems I've already come across, but I'd like to generally find out about what are some best practices with forms.

like image 508
Monika Sulik Avatar asked Nov 24 '09 12:11

Monika Sulik


People also ask

What is Django Python used for?

What is Django? Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.

How can I get form data in Django?

Using Form in a View In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin.

What are forms in Django?

Django provides a Form class which is used to create HTML forms. It describes a form and how it works and appears. It is similar to the ModelForm class that creates a form by using the Model, but it does not require the Model.


1 Answers

I've asked similar questions myself. There's a lot of room for improvement in Django's form handling, and there is some discussion of doing something about that but it isn't moving very quickly. Your asking this question validates this area needs some attention.

That said, the current ways (to the best of my knowledge) to handle those scenarios is:

  1. The easiest way is to use a ModelForm for the Partnership and inlineformset for the People. It isn't too difficult, but be aware that there's no easy way to combine them into one form object so you'll have to handle them separately.
  2. Not exactly sure what scenario you have in mind, but I think inline formsets are the way to go here as well.
  3. Django has support for this as of 1.0: http://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/
  4. I'm working on an app to do exactly this (with help from jQuery). I've been meaning to clean it up and put it on GitHub, and any extra interest will certainly speed up that process.

Inline Formsets: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets

like image 105
John Debs Avatar answered Oct 05 '22 15:10

John Debs