将动态表单数据从模板传输到Django中的视图?

时间:2018-06-26 11:57:18

标签: json django-forms django-templates django-views

我正在制作计费系统,因为可以从数据库中获取商品的数量及其价格,这些商品可以在index.html模板中查看,在 index.html 文件中,我可以输入字段中,我以单位输入项目数,如何在视图中发送项目数

index.html

<form method='POST' action="{% url 'bill:total_amount' %}" name= 'myform'>
   {% csrf_token %}
   <table>
    <tr>
      <th>Items-Name</th>&nbsp
      <th><center>Quantity</center></th>&nbsp
      <th><center>Price[Per_units]</center></th>
    </tr>
    {% for p in products %}
    <tr>
      <td>{{ p.product_name }}</td>&nbsp
      <td><input type="number" min="0" name="quantity" value="0"></td>&nbsp
      <td><center>{{ p.product_price }}</center></td>
    </tr>
    {% endfor %}                            
   </table>
   <button type="submit" class="btn btn-default">Submit</button>
</form>

views.py

def total_amount(request):
   #how to fetch all the values here from form
   quantity = request.POST.get('quantity')
   #arithmetic operation to be done
   return render(request, "bills/bills.html", {})

0 个答案:

没有答案
相关问题