How to insert two different forms on one page in Django

I need to insert 2 forms on one page:

1) Registration form

2) Login form

.

So, if I use this in 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 an error by submitting one of two forms.

How can I distinguish between the two forms presented in the views?

+4
source share
6 answers

You can also do this,

  <form method='POST'> {{form1.as_p}} <button type="submit" name="btnform1">Save Changes</button> </form> <form method='POST'> {{form2.as_p}} <button type="submit" name="btnform2">Save Changes</button> </form> 

CODE

 if request.method=='POST' and 'btnform1' in request.POST: do something... if request.method=='POST' and 'btnform2' in request.POST: do something... 
+5
source

You can submit two forms on the same page ... but the action that each form invokes (i.e. the presentation function that will process each form) is likely to be different. Thus, you will not need to distinguish between forms.

eg. 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> 

So, in views.py you will have an input () function and a view () view function that will handle each of these forms.

+3
source

You can also send both forms to the same URL:

forms in the template:

 <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 treat them like this:

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

and then do things with firstform & secondform.

+2
source

You can register and register POST at different URLs so that each POST is processed by the corresponding representation

+1
source

You can place both forms on the same URL and have hidden input with the name set for login or registration and sorting on the server

+1
source
  <form action="Page where u want to post the data" method="post"> <input name="edit" type="submit" value="Edit Client"> <input name="delete" type="submit" value="Delete Client"> </form> 

just enter different names for the buttons.

 if request.method == "POST" and 'edit' in request.POST: / Do / if request.method == "POST" and 'delete' in request.POST: /Do / 
+1
source

Source: https://habr.com/ru/post/1308659/


All Articles