Joker
Joker

Reputation: 2229

Using forms only for validation

Say you already have a complex HTML form, possible from a designer, front end dev, etc. Is it common practice to not use dynamic forms (based on a Django form) for complicated forms?

I want to do something like this:

1.) Create custom HTML form.

2.) Catch form data through POST request, put it in an object/dictionary.

3.) Do some manipulations with that data to get it in a format acceptable by a Django form.

4.) Pass the manipulated data in to a form object, validate it, etc...

What is a clear solution to this problem? Should I be using Django's dynamic forms for everything? If not - how do I implement the above?

EDIT:

Part of my question has to do with using the forms ONLY for validation. I don't think I made this clear. Here is what I'm trying to do:

template.html

<form method="post">
    {% csrf_token %}
<input class="foo" name="bar" type="text" value=""/>
<!-- Some more fields, not rendered through Django form -->
<button type="submit">Create Object</button>
</form>

As you can see, other than the csrf_token there is no Django code here. What I am trying to do in my view is catch the data in the POST in my view, make some changes to the data, then try to bind the new data to a form (not sure if it's possible):

views.py

def my_view(request):
    # Some GET code

    if request.method == 'POST':
        form = ImportedForm(request.POST)
        form.data['foo'] = "newValue"
        # Now after changing the data, validate it...

Upvotes: 1

Views: 90

Answers (2)

Joker
Joker

Reputation: 2229

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

Upvotes: 0

John Mee
John Mee

Reputation: 52233

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:

  1. create a django form with all the field definitions
  2. create django GET view to serve the empty form
  3. create an html template which serves the default html/form
  4. test the blank form
  5. create the POST routine to call validation and reserve the validated (erroneous) form
  6. modify the django form to validate the fields
  7. modify the html form to serve the error messages
  8. test the validation and error messages
  9. 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)
  10. Test the whole lot
  11. 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).

Upvotes: 1

Related Questions