【问题标题】:Django: Get value from html for-loop for views.pyDjango:从views.py的html for循环中获取价值
【发布时间】:2021-11-24 14:25:46
【问题描述】:

我一直坚持从 html 页面的 for 循环中获取值。

这是我的设置:用户有一个使用自动完成功能的输入字段。此函数使用 Django 中的 shop-Model。提交后,产品将被添加到 Django 中的 Shopping_List-Model 中。它将显示在页面的另一个 div 中。这目前可以正常工作。

每个产品都在一个表格的一行中,包括一个用于从购物清单中删除该产品的按钮。这样的按钮正在工作。单击按钮时,我试图打印出一些东西。但我无法从表行中获取值,即产品名称。它总是不返回任何内容。有了这个值,我想从购物清单中删除产品。

非常感谢任何建议!

这是我的代码:

HTML

<div>
<!--Input field and autocomplete-->
<form id="product_add" action="{% url 'shop' %}" method="post">
{% csrf_token %}
<input type="text" name="shop_product" id="shop_product">

   <!--Code for autocomplete-->
   <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
   <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
   <script>
      $(function () {
         $("#shop_product").autocomplete({
            source: "{% url 'shop' %}",
            minLength: 2
            });
         });
   </script>
<input type="submit" name="button_add" value="Add">
</form>
</div>

<div>
<table>
{% for item in items %}
<tr>
   <form id="shop_delete" action="{% url 'shop_delete' %}" method="post">
      <td name="product" id="product">{{ item.product }}</td>
      <td><button type="submit" name="delete">X</button></td>
   </form>
</tr>
{% endfor %}
</table>
</div>

views.py

def shop(request):
    if request.method == "POST":
        if request.POST["button_add"] == "Add":
            user = request.user
            product = request.POST["shop_product"]
            date=datetime.date.today()

            if product != "": Shopping_List.objects.create(user=user, date=date, product=product)
        else:
            print("delete")

    if 'term' in request.GET:
        qs = Shop.objects.filter(product__icontains=request.GET.get('term'))
        items = list()
        for product in qs:
            items.append(product.product)
        # titles = [product.title for product in qs]
        return JsonResponse(items, safe=False)
    
    items = Shopping_List.objects.all().filter(user=request.user, date=datetime.date.today())
    return render(request, "shop.html", {
        "items": items
    })

def shop_delete(request):
    if request.method == "POST":
        product = request.POST.get("product")
        print(product)
    return redirect('shop')

【问题讨论】:

    标签: django for-loop http-post


    【解决方案1】:

    您的表单不包含任何输入,因此,POST 请求不会提交任何输入数据。

    #This is not an input
    <td name="product" id="product">{{ item.product }}</td>
    

    当您使用有效的表单input 时,您将能够在视图中检索产品名称。

    例如,您可以添加一个带有产品名称的隐藏字段。那么你就不需要重新设计你的表格元素了。

       <form id="shop_delete" action="{% url 'shop_delete' %}" method="post">
          <td id="product">{{ item.product }}</td>
          <input type="hidden" value="{{item.product}}" name="product">
          <td><button type="submit" name="delete">X</button></td>
       </form>
    

    【讨论】:

    • 非常感谢!正是我需要的!
    猜你喜欢
    • 2018-05-21
    • 2021-04-01
    • 2015-11-01
    • 1970-01-01
    • 2017-01-15
    • 2021-02-28
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    相关资源
    最近更新 更多