【发布时间】:2019-08-17 05:07:37
【问题描述】:
我刚开始学习 Django,并按照 Antonio Mele 在他的书“Django 2 by Example”中的教程设置了一个简单的购物车。我可以选择产品,然后数量将是一个选项。这适用于通用产品。如果我有刻字笔等定制产品怎么办?买家选择数量为 3,但他希望在笔上刻上不同的名字。我该怎么做?
这里是forms.py控制数量的代码。
from django import forms
PRODUCT_QUANTITY_CHOICES = [(i, str(i)) for i in range(1, 11)]
class CartAddProductForm(forms.Form):
quantity = forms.TypedChoiceField(choices=PRODUCT_QUANTITY_CHOICES, coerce=int)
update = forms.BooleanField(required=False, initial=False, widget=forms.HiddenInput)
HTML 模板的相关部分看起来像
{% for item in cart %}
{% with product=item.product %}
<tr>
<td>{{ product.name }}</td>
<td>
<form action="{% url "cart:cart_add" product.id %}" method="post">
{{ item.update_quantity_form.quantity }}
{{ item.update_quantity_form.update }}
<input type="submit" value="Update">
{% csrf_token %}
</form>
</td>
<td><a href="{% url "cart:cart_remove" product.id %}">Remove</a></td>
<td class="num">${{ item.price }}</td>
<td class="num">${{ item.total_price }}</td>
</tr>
{% endwith %}
{% endfor %}
【问题讨论】:
标签: django python-3.x django-forms