Django多个表单,一个提交按钮 [英] Django multiple forms with one submit button

查看:127
本文介绍了Django多个表单,一个提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的webapp中有很多民意调查,我把它们都显示在同一页面上,但无法弄清楚如何为所有表单提交(投票)按钮。到目前为止,他们都有自己的投票按钮,每个问题,但我只想一个底部提交一切。



此模板的HTML代码如下:

  %if latest_question_list%} 
{%for latest_question_list%}
< h2> {{question.question.text}}< / h2>
< form action ={%url'polls:vote'question.id%}method =post>
{%csrf_token%}
{%for question.choice_set.all%}

< input type =radioname =choiceid =choice {{for loop.counter}}value ={{choice.id}}/>
< label for =choice {{forloop.counter}}> {{choice.choice_text}}< / label>< br />

{%endfor%}

< input type =radioname =choicevalue =>其他:< input type =text />
< br>< input type =submitvalue =Vote/>< / br>
< / form>
{%endfor%}

{%else%}
< p>没有轮询可用。< / p>
{%endif%}

有没有办法做到这一点?我感谢任何帮助!谢谢。

解决方案

请参阅 - django提交两个不同的表单与一个提交按钮



Django的表单类,当提供的数据从请求,查看POST或GET通过元素id查找值。表单中的所有字段必须具有唯一的ID,或使用在您的视图中构建表单时,Django表单的前缀关键字。简单地说,将所有的表单元素放在单个html标签中,使用前缀关键字(每个Django表单都有唯一的前缀),任何时候在视图;这将改变输入/ select / radiobox元素的id属性),然后从那里。向每个表单实例提供GET或POST,并且

您的好消息。



但是,上面的代码显示了手动填充每个字段的ID。相反,为什么不将每个表单传递给模板作为{{form_name.as_p}}包含?除非你有很好的理由去做,否则你可能会为自己造成头痛。


I have many "polls" in my webapp, in which I have them all showing up on the same page, but cannot figure out how to have one submit(vote) button for all of the forms. As of right now, they all have their own vote buttons for each question, but I would like just one at the bottom to submit everything.

My HTML code for this template is as follows:

{% if latest_question_list %}
  {% for question in latest_question_list %}
  <h2>{{ question.question.text }}</h2>
  <form action="{% url 'polls:vote' question.id %}" method="post">
  {% csrf_token %}
  {% for choice in question.choice_set.all %}

      <input type="radio" name="choice" id="choice{{ for loop.counter }}" value="{{ choice.id }}" />
      <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />

   {% endfor %}

   <input type="radio" name="choice" value="">Other: <input type="text" />
   <br><input type="submit" value="Vote" /></br>
   </form>
   {% endfor %}

{% else %}
   <p>No polls are available.</p>
{% endif %}

Is there an easy way to do this? I appreciate any help! Thanks.

解决方案

See - django submit two different forms with one submit button

Django's form class, when supplied data from the request, looks at POST or GET to find values by element id. All fields within the form must have unique id's or use the Django form's prefix keyword when constructing the form in your view.

In short - put all your form elements within a single html tag, use the prefix keyword (with unique prefixes for each Django form, any time the form is constructed in the view; this will alter the input/select/radiobox elements' id attribute ), and go from there. Supply the GET or POST to each form instance and
your good to go.

However, your code above shows you manually populating the id of each field. Rather, why not pass each form to the template as include as {{ form_name.as_p }} ? Unless you have a good reason to do otherwise, you are probably creating a headache for yourself.

这篇关于Django多个表单,一个提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆