Python Django表格更新数量
我使用Django表单来更新一个项目的数量,虽然在我看来,我已经正确地构建了所有内容,但当我单击“提交”时,表单实际上并没有更新数量。当然,我也没有收到任何错误——就好像表单被接受一样 在forms.py中:Python Django表格更新数量,python,django,django-forms,e-commerce,Python,Django,Django Forms,E Commerce,我使用Django表单来更新一个项目的数量,虽然在我看来,我已经正确地构建了所有内容,但当我单击“提交”时,表单实际上并没有更新数量。当然,我也没有收到任何错误——就好像表单被接受一样 在forms.py中: class ChangeQty(forms.Form): quantity = forms.IntegerField(label='', min_value=1, widget=forms.NumberInput()) class Meta: model
class ChangeQty(forms.Form):
quantity = forms.IntegerField(label='', min_value=1, widget=forms.NumberInput())
class Meta:
model = Item
fields = 'quantity'
def __init__(self, *args, **kwargs):
super(ChangeQty, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_show_labels = False
self.helper.form_id = 'id-exampleForm'
self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
self.helper.form_action = 'submit'
self.helper.add_input(Submit('submit', 'Update', css_class='btn btn-md btn-primary'))
self.helper.layout = Layout(
Field('quantity', placeholder='Qty'),
)
在cart.py中
def add(self, product, unit_price, quantity):
try:
item = models.Item.objects.get(
cart=self.cart,
product=product,
)
except models.Item.DoesNotExist:
item = models.Item()
item.cart = self.cart
item.product = product
item.unit_price = unit_price
item.quantity = 1
item.save()
else: #ItemAlreadyExists
item.unit_price = unit_price
item.quantity = int(quantity)
item.save()
In views.py
def specific_qty(request, product_id, quantity):
if request.method == 'POST':
form = ChangeQty(request.POST)
if form.is_valid():
cart = Cart(request)
cart.add(product_id, product.price, form.get('quantity'))
return render_to_response("full_cart.html", dict(cart=Cart(request)), context_instance=RequestContext(request))
else:
form = ChangeQty()
return render(request, 'full_cart.html', {'form': form})
最后,在full_cart.html中:
<li>Quantity:
<form class="col-md-8 col-md-offset-2" action="." method="post">
{% csrf_token %}
<input id="quantity" type="text" name="quantity" value="{{item.quantity}}">
<input class='btn btn-md btn-primary' type="submit" value="Update">
</form>
</li>
数量:
{%csrf_令牌%}
erm不应该是表单。已清理的\u数据['quantity']?为什么不在代码中随意地添加一些日志消息来找出发生了什么,或者在调试器下运行它呢?什么是Cart
?您正在创建新的Cart
对象Cart=Cart(请求)
,并且从未保存它。Cart
是如何保存在Item
中的?erm不应该是表单吗?数据['quantity']已清理?为什么不在代码中随意地添加一些日志消息来找出发生了什么,或者在调试器下运行它呢?什么是Cart
?您正在创建新的Cart
对象Cart=Cart(请求)
,并且从未保存它。如何将购物车
保存在项目
中?