views:

59

answers:

3

I have to insert 2 forms in the same page:

1) Registration form

2) Login form

.

So if I use this in the views.py:

    if request.method == 'POST':

        form = registrationForm(request.POST) 
        if form.is_valid():  
            form.save()    
            return render_to_response('template.html', {
    'form': form,
})

I will get error by submitting one of two forms.

How can I distinguish the 2 forms submitting in the views ?

+1  A: 

You can do the Registration and Login POST to different urls so each POST will be handled by corresponding view

dragoon
+2  A: 

You can submit two forms on the same page... but the action that each form calls (i.e. the view function that will process each form) should probably be different. That way, you won't have to try and distinguish the forms.

e.g. On your page:

<form id="login_form" action="{% url app.views.login %}" method="post">

   ...form fields...

</form>

<form id="registration_form" action="{% url app.views.registration %}" method="post">

   ...form fields...

</form>

And so, in views.py, you'll have a login() view function and a registration() view function that will handle each of those forms.

kchau
A: 

You can post both forms to same url too:

forms in template are like this:

<form method="post" action="/profile/">
{% for field in firstform %}
    <div class="mb10">
    <div class="fl desc">{{ field.label_tag }}<br />
    <div class="fr">{{ field }}{{ field.errors }}</div>
    <div class="clear"></div>
    </div>  
{% endfor %}
{% for field in secondform %}
    <div class="mb10">
    <div class="fl desc">{{ field.label_tag }}<br /><</div>
    <div class="fr">{{ field }}{{ field.errors }}</div>
    <div class="clear"></div>
    </div>  
{% endfor %}
<a class="submit fr" href="#""><img src="{{ MEDIA_URL }}img/save.png" /></a>
</form>

and just handle them like this in view:

if request.method == 'POST':
    firstform = ProfileForm(request.POST, request.FILES, instance=profile)
    secondform = UserForm(request.POST, instance=request.user)

and then do stuff with firstform&secondform.

Zayatzz