1👍
If the form and model match nicely then I’ll take advantage of the ModelForm functionality.
But most of the time it is not so tidy so, most typically, I do things in about this order:
- create a django form with all the field definitions
- create django GET view to serve the empty form
- create an html template which serves the default html/form
- test the blank form
- create the POST routine to call validation and reserve the validated (erroneous) form
- modify the django form to validate the fields
- modify the html form to serve the error messages
- test the validation and error messages
- modify the POST routine to handle a valid form and do whatever it should do as a result (might involve a redirect and ‘thanks’ view/template)
- Test the whole lot
- let the designer loose on the templates
In truth the designer will be involved at some points earlier along the way but in theory I just get it all to work as a “white” then add all the fancy stuff after. That includes javascript validation (ie after all the above).
0👍
I ended up doing something like this. It is ugly, and may not be the proper way to do it, but it works…
if request.method == 'POST':
try:
# Create dictionary from POST data
data = {
'foo': request.POST['foo'],
'foobar': request.POST['foobar'],
}
except:
# Handle exceptions
form = ImportedForm(data)
if form.is_valid:
# Continue to validate and save
- [Answer]-Django, set csrf token on form in static pages
- [Answer]-Aggregation and filtering in Django
Source:stackexchange.com