Django 如何使用循环中的多个按钮创建一个表单
这是我在模板中的表格, 我正在创建多个表单,循环中有两个按钮用于投票特定项目 我觉得这很难看,我怎么能避免所有按钮只使用一种形式呢Django 如何使用循环中的多个按钮创建一个表单,django,django-forms,django-templates,django-views,Django,Django Forms,Django Templates,Django Views,这是我在模板中的表格, 我正在创建多个表单,循环中有两个按钮用于投票特定项目 我觉得这很难看,我怎么能避免所有按钮只使用一种形式呢 {% for bill_item in bill_items %} <form action="{% url 'bills:change_quantity' bill_item.id %}" method="post"> {% csrf_token %} <button name="up"></button
{% for bill_item in bill_items %}
<form action="{% url 'bills:change_quantity' bill_item.id %}" method="post">
{% csrf_token %}
<button name="up"></button>
<button name="down"></button>
</form>
{% endfor %}
将bill_item.id移到按钮上如何?目前无法对此进行测试,因此请将其视为未经检查的建议 比如:
{枚举中的票据项(票据项)%}
def change_quantity(request, bill_item_id):
bill_item = BillItem.objects.get(pk=bill_item_id)
if 'up' in request.POST:
bill_item.increase()
elif 'down' in request.POST:
bill_item.decrease()
bill_item.save()
return HttpResponseRedirect('/bills/')
<form action="{% url 'bills:change_quantity' bill_item.id %}" method="post">
{% for x_id,bill_item in enumerate(bill_items) %}
<button name={% x_id %} value="up"></button>
<button name={% x_id %} value="down"></button>