簡體   English   中英

從Django中的模板返回Request.Post數據

[英]Returning Request.Post data from template in Django

我正在嘗試使用request.post獲取購物車中單個產品的數量,但不返回任何東西。

這是我的views.py文件的樣子:

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

這是我的模板目前的樣子:

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

我希望控制台打印出request.post條目的數量。

提前致謝。

一旦將HTML更改為以下內容,該HTML是不正確的-它開始起作用。 這是由於輸入在for循環之外。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM