Hi Franco, Faridul, Stodge,

There are actually quite a few problems being asked. I've split them up into 
discrete questions.

1. Can a single view function print three forms in a template?

Yes.

WARNING: I am coding all of this off the top of my head. The code is likely 
riddled with typos and errors. Sorry. Hopefully the example will demonstrate 
enough of the idea.

    # VIEW FUNCTION
    # Start by invoking the forms you want
    # remember to pass POST or initial data if necessary 
    form1 = FormClassOne()
    # etc for as many forms as you'd like.

    # Create a Context or Request Context
    c = Context({"foo": [form1, form2, form3]})

    # or, more easily, use the `render` shortcut
    render(
        request,
        template_name_vsr,
        {'forms' : [form1, form2, form3]})

    # TEMPLATE
    <div id="tabs-set">
    {% for form in forms %}
      <div class="tab">
        <form action="{# this depends on the next answers! #}" method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Submit" />
        </form>
      </div>
    {% endfor %}
    </div>

2. How should I handle the submission process (POST data)? With one view 
function, or with three view functions?

Both are valid ways of doing it, but I heavily favor the method with three POST 
processing views. The reason is that by combining all this logic in one 
location, you are increasing the complexity of your site drastically. Debugging 
that will be no fun in the event of a problem. Having a place where the forms 
are as simple as possible is quite desirable. Remember to write tests.

I would recommend starting with the three view method, and then moving into the 
single view function method, if so desired.

3. How do I program three view functions to handle POST requests from forms in 
a single view function?

Start by creating a basic view function that handles both the GET (display of 
form) and POST (submission of data) for all of the forms (just as you would 
regularly). A Generic Class Based View might be all you need here, but it might 
be wise to start with a simple Class Based View.

Now, simply point the forms above to these webpage urls. There are many ways to 
do this. I would consider: (1) adding the action URL to the form class as an 
instance variable, or else (2) simply building the URL in the single form view 
and passing it to the URL.

Note: you may have/want to account for the URL referring to these forms. I am 
fuzzy on the details

Consider that having a separate view/page for each form will help debug any 
issues, as you have a place where the complexity of the form/view is as low as 
possible.

4. How do I program a single view function to handle POST requests from all 
three forms?

The first problem is that you need to know which form has been submitted from 
the single view page. Using a hidden form field may provide that information, 
or checking the keys of the POST querydict may also work. However, in both 
cases, you are relying on information provided by the user, which is a Bad 
Idea. This is the biggest reason not to follow this method.

However, once you know which form is being submitted, you may then process it 
according to the rules set forth in the three views your previously programmed. 
Remember to adhere to DRY (ie: split the functionality into small, reusable 
pieces)!

I realize that's a lot of info, but again, there was quite a lot being asked. I 
hope that helps clarify the issue.

Andrew

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/24D6792A-4316-4806-8D10-470EAD15BCFD%40andrewsforge.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to