简体   繁体   中英

Returning Request.Post data from template in Django

I am trying to get the quantity of single product in cart using request.post but its not returning anything.

This is what my views.py file looks like:

def test_view(request):
    cart_obj, new_obj = Cart.objects.new_or_get(request)
    my_carts_current_entries = Entry.objects.filter(cart=cart_obj)
    entry_quantity = request.POST.get('entry_quantity')
    print(entry_quantity)
    return render(request, 'carts/test.html', {'my_cart': cart_obj, 'my_carts_current_entries': my_carts_current_entries})

This is what my template looks like at the moment:

<!doctype html>
<html>
    <head>

    </head>
    <body>
    <h4>These are the current items in your Basket</h4>



    <form method="POST"> 
        {% csrf_token %}
        {% for cart in  my_carts_current_entries %}
        {{ cart.quantity }} x {{ cart.product }} <br>
        {% endfor %}
        <input type="hidden" name='entry_quantity' value='{{ cart.quantity }}'>
        <button>Add to Basket</button>
    </form>

    </body>
</html>

I am expecting a console printout of the request.post entry quantity.

Thanks in advance.

The HTML was incorrect, once it was changed to the below - it started working. This was due to the fact that the input was outside the for loop.

{% csrf_token %}
{% for cart in  my_carts_current_entries %}
{{ cart.quantity }}x {{ cart.product }}
<button>Generate (Quantity)</button>
<input type="hidden" name='entry_quantity' value='{{ cart.quantity }}'>
<br>
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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