Html Django具有一个提交按钮的多个表单
我在我的webapp中有很多“民意调查”,我把它们都显示在同一个页面上,但我不知道如何为所有表单设置一个提交(投票)按钮。到目前为止,他们每个问题都有自己的投票按钮,但我只希望底部有一个按钮来提交所有内容 此模板的HTML代码如下所示:Html Django具有一个提交按钮的多个表单,html,django,django-templates,Html,Django,Django Templates,我在我的webapp中有很多“民意调查”,我把它们都显示在同一个页面上,但我不知道如何为所有表单设置一个提交(投票)按钮。到目前为止,他们每个问题都有自己的投票按钮,但我只希望底部有一个按钮来提交所有内容 此模板的HTML代码如下所示: {% if latest_question_list %} {% for question in latest_question_list %} <h2>{{ question.question.text }}</h2> &l
{% 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 %}
{%if最新问题列表%}
{最新问题列表%中的问题%
{{question.question.text}
{%csrf_令牌%}
{问题中的选项为%choice\u set.all%}
{{choice.choice_text}}
{%endfor%}
其他:
{%endfor%}
{%else%}
没有投票
{%endif%}
有没有一个简单的方法可以做到这一点?谢谢你的帮助!谢谢 见-
Django的表单类在从请求中提供数据时,通过元素id查看POST或GET以查找值。表单中的所有字段都必须具有唯一id,或者在视图中构造表单时使用
简言之,将所有表单元素放在一个html标记中,使用prefix关键字(在视图中构造表单时,每个Django表单都有唯一的前缀;这将改变input/select/radiobox元素的id属性),然后从那里开始。向每个表单实例提供GET或POST,并你很高兴去 但是,上面的代码显示手动填充每个字段的id。相反,为什么不将每个表单作为include as{{form_name.as_p}传递给模板呢?除非你有很好的理由不这样做,否则你可能会让自己头疼