1

It is my form in template, I am creating multiple forms with two buttons in the loop for voting particular item and i think it is ugly, how can i avoid that using only one form for all buttons?

{% 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 %}

It is my action in the view

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/')
0

1 Answer 1

3

How about moving bill_item.id to button? Can't test this at the moment, so please treat this as unchecked suggestion

Like:

<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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.