【问题标题】:how to differentiate two forms in a django template?如何区分 django 模板中的两种形式?
【发布时间】:2021-01-05 16:54:57
【问题描述】:

我直接在 django html 模板中创建表单。 在这个模板中,我有 2 个表单。

{% 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 %}

在 views.py 中,我试图与表单区分开来。 我已经要求只出现在第一个表单中的字段

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)

是否有其他方法可以知道发出 post 请求的表单是什么? 也许添加一个隐藏的输入?仅出于此目的,我想知道哪个是最佳做法。

【问题讨论】:

  • 在表单中添加 url
  • 没有必要,因为默认情况下django将post请求发送到页面的相同url。然后在视图中我可以管理发布请求。

标签: python html django forms templates


【解决方案1】:

我在两种形式中都添加了带有name="form_id" 的隐藏字段

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

在视图中我要求它:

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

【讨论】:

    【解决方案2】:

    您可以将提交按钮命名为name="form1submit",然后通过在request.POST 字典中查找此名称来询问是否按下了此按钮。

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

    【讨论】:

      猜你喜欢
      • 2011-01-07
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 2011-11-14
      相关资源
      最近更新 更多