简体   繁体   English

Django-使用一个提交按钮提交多个表单

[英]Django - Submit multiple forms with one submit button

I'm trying to create a check in page that lists all of the people that have signed up. 我正在尝试创建一个签入页面,该页面列出所有已注册的人。 Each person has an input box that submits the number of hours they were at the event. 每个人都有一个输入框,用于提交他们参加活动的小时数。 I want it so that the same form is submitted multiple times for every person; 我想要这样,以便每个人都可以多次提交相同的表格; I don't want to have a submit button for every person because there will be several people and it'll be tedious. 我不想为每个人都有一个提交按钮,因为会有几个人,这将很乏味。 I'm using a for loop {% for signups in signup %} to loop through the queryset for the people who are signed up. 我正在使用for循环{% for signups in signup %}来循环{% for signups in signup %}人员的查询集。

Here's a screenshot to illustrate: 这是说明的屏幕截图:

In the backend, I want it to save the number of hours to the row in the queryset with the matching name. 在后端,我希望它将小时数保存到查询集中具有匹配名称的行中。

HTML: HTML:

<form action="/events/occ_checkin" class="form" method="POST" id="checkin_{{ signups.id }}" name="checkin_{{ signups.id }}">{% csrf_token %}
    {% for form in formset %}
    <h5>
    <label for="{{ form.fullname.id_for_label }}">***how would I get the attendee's name?***</label>
    <input id="{{ form.fullname.id_for_label }}" name="{{ form.fullname.html_name }}" type="hidden" value="***attendee's name here as well***">
    <input id="{{ form.hours.id_for_label }}" name="{{ form.hours.html_name }}" step="0.01" type="number" class="form-control" value="{{ events.hours }}">
    </h5>
    {% endfor %}
    <button class="btn btn-primary btn-block" type="submit">Submit</button>
</form>

Using modelformset_factory worked. 使用modelformset_factory起作用。

HTML: HTML:

<form action="/events/occ_checkin" class="form" method="POST" id="checkin_{{ signups.id }}" name="checkin_{{ signups.id }}">{% csrf_token %}
    {% for form in formset %}
    <h5>
    <label for="{{ form.fullname.id_for_label }}">{% for signups in signup %}{% if forloop.parentloop.counter == forloop.counter %}{{ signups.fullname }}{% endif %}{% endfor %}</label>
    <input id="{{ form.fullname.id_for_label }}" name="{{ form.fullname.html_name }}" type="hidden" value="{% for signups in signup %}{% if forloop.parentloop.counter == forloop.counter %}{{ signups.fullname }}{% endif %}{% endfor %}">
    <input id="{{ form.hours.id_for_label }}" name="{{ form.hours.html_name }}" step="0.01" type="number" class="form-control" value="{{ events.hours }}">
    </h5>
    {% endfor %}
    <button class="btn btn-primary btn-block" type="submit">Submit</button>
</form>

For views.py and forms.py I just followed the documentation on formsets. 对于views.py和forms.py,我只是遵循了表单集上的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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