【问题标题】:How to create one form with multiple buttons in the loop如何在循环中创建一个带有多个按钮的表单
【发布时间】: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


    【解决方案1】:

    将 bill_item.id 移动到按钮怎么样?目前无法对此进行测试,因此请将此视为未经检查的建议

    喜欢:

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多