简体   繁体   中英

how to differentiate two forms in a django template?

I'm creating forms directly in a django html template. In this template I have 2 forms.

{% extends 'base.html' %}

{% block title %}
   <title>Add Groups</title>
{% endblock %}


{% block content %}

<h1>Add Groups</h1>

<form method="post">
   {% csrf_token %}
   <label>User</label>
   <input type="text" name="user_name"/>
   <label>Password</label>
   <input type="text" name="password"/>

   <input type="submit" value="Submit">
</form>

{% if groups %}
   <form method="post">
       {% csrf_token %}
   {% for group in groups %}

       <div class="div_group">
           <h3>
               <input type="checkbox" name="{{group.id}}" value="{{group.name}}">

               {{group.name}}</h3>
           <div> 
               <span>Id:</span>
               {{group.id}}
               <br>
               <a href="{{group.link}}">{{group.link}}</a>
               <br>
               <span>Role:</span>
               {{group.role}}
               <br>
               <span>Tags:</span>
               {{group.tags}}

           </div>
       </div>

   {% endfor %}
       <input type="submit" value="Add Grupos">
   </form>
{% endif %}

<br>

{% endblock %}

In the views.py I'm trying to differentiate from the forms. I've made it asking for fields that only appear in the first form

def admin_add_group(request):
   if request.user.is_staff and request.user.is_staff:
       context = {}

       if request.method == 'POST':
           if 'user_name' in request.POST:
               user_name= request.POST['user_name']
               mendeley_password = request.POST['password']    
               
               try:
                   # login mendeley
                   md.authenticate(mendeley_user, mendeley_password)
                   groups = md.get_groups()
                   context['groups'] = groups
               except Exception as exc:
                   context['errors'] = [str(exc)]
           else:
               c = request.POST
               print(c)
               # redirect to groups view.

       return render(request, 'admin_add_group.html', context)

   else:
       context = {}
       return render(request, 'admin_add_group.html', context)

Is there another way to know what form is making the post request? maybe to add a hidden input? only for this purpose, I'd like to know which is the best practice.

I added a hidden field with name="form_id" in both forms

 <form method="post">
    {% csrf_token %}    
    <input type="hidden" name="form_id" value="groups_data">
    ...

<form method="post">
    {% csrf_token %}
    <input type="hidden" name="form_id" value="user_data">
    ...

and in the view I asked for it:

 if request.method == 'POST':
        if request.POST['form_id'] == 'user_data':
        ...

You can name the submit buttons name="form1submit" and then ask if this button was pressed, by looking for this name in the request.POST dict.

if 'form1submit' in request.POST:
    # is the form 1
else:
    # is the form 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM