【发布时间】:2013-07-24 16:10:00
【问题描述】:
这是我在模板中的表格, 我正在使用循环中的两个按钮创建多个表单以对特定项目进行投票 我认为这很丑陋,我怎样才能避免所有按钮只使用一种形式?
{% 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 %}
这是我在视图中的操作
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/')
【问题讨论】:
标签: django django-forms django-templates django-views