How insert 2 different forms on the same page in Django

北战南征 提交于 2019-12-30 05:11:06

问题


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:


You can also do like 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...



回答2:


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.




回答3:


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.




回答4:


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




回答5:


You can have both forms posting to the same URL and have a hidden input with name set to login or registration and sort that out on the server




回答6:


 <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 Give different names to the buttons.

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


来源:https://stackoverflow.com/questions/2758330/how-insert-2-different-forms-on-the-same-page-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!