所以我正在开发一个电子商务应用程序(与在线订购食品有关),目前我可以在订单中添加商品,但不能添加多个商品。我真的不明白这是怎么回事。 在我看来,
def show_item(request,id):
# need to evaluate the HTTP method
if request.method == 'POST'
a = Item.objects.get(pk=id)
form = PartialOrderItemForm(request.POST,instance=a)
# check validation of posted data
if form.is_valid():
order.add_to_order(request,a)
# if test cookie worked, get rid of it
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
url =urlresolvers.reverse('order_index')
# redirect to order page
return HttpResponseRedirect(url)
else:
form = PartialOrderItemForm()
request.session.set_test_cookie()
context={
'form':form,
}
return render_to_response('item.html',context,context_instance=RequestContext(request))
验证表单后,视图调用此方法,该方法负责添加项目
def add_to_order(request,obj):
postdata = request.POST.copy()
#get quantity added, return 0 if empty
quantity = postdata.get('quantity',0)
# fetch the item or return missing page error_message
i = get_object_or_404(Item,pk=obj.id)
i.orderitem_set.all()
# get items in order
order_items = get_order_items(request)
item_in_orders = False
# check to see if item is already in order
for order_item in order_items:
if order_item.item.id == i.id:
#update the quantity if found
order_item.augment_quantity(quantity)
item_in_orders = True
if not item_in_orders:
# creat and save a new order item
oi = OrderItem()
oi.order_id = _order_id(request)
oi.quantity = quantity
oi.item = i
oi.save()
这是我在模板中的表单..
<form method="post" action=".">
{% csrf_token %}
{{ form.as_p }}
<br />
<input type="submit" id="submit_order" value="Add To Order" name="submit">
</form>
关于我应该怎么做的任何建议。
答案 0 :(得分:0)
问题出在我的缩进上,只能用pdb找到它。
pdb
是真正的交易