我有这三个功能,我把它们安排得很远
def show_order(request,id):
order = Order.objects.get(pk=id)
if request.method == 'POST':
postdata = request.POST.copy()
if postdata['submit'] == 'Remove':
order.remove_from_order(request)
if postdata['submit'] == 'Update':
order.update_order(request)
if postdata['submit'] == 'checkout':
"""once confirmed order, click checkout and redirect to checkout_form"""
return HttpResponseRedirect(urlresolvers.reverse('checkout',kwargs={'id':order.id}))
....
def show_checkout(request,id):
if request.method == 'POST':
resto = Restaurant.objects.get(pk=id)
postdata = request.POST.copy()
form = forms.CheckoutForm(request.POST,postdata)
if form.is_valid():
"""create order after validating form"""
order_created = create_order(request,resto)
order_number = order_created.id
if order_number:
request.session['order_number'] = order_number
if postdata['submit'] == 'place order':
reciept_url = urlresolvers.reverse('checkout_reciept')
return HttpResponseRedirect(reciept_url)
......
def create_order(request,obj):
"""create new online_order containing each orderitem instance,save order and empty the order."""
anon_user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
resto = Restaurant.objects.get(pk=obj.id)
orders = Order()
checkout_form = forms.CheckoutForm(request.POST,instance=orders)
checkout = checkout_form.save(commit=False)
checkout.created_by = anon_user
checkout.modified_by = anon_user
checkout.restaurant = resto
checkout.status = Order.SUBMITTED
checkout.save()
if checkout.pk:
"""if the order save suceeded"""
for ci in cart_items:
"""create order for each order_item"""
........
这是网址
url(r'^your_order/(?P<id>\d+)/$',show_order,name="order_index"),
url(r'^checkout/(?P<id>\d+)/$',show_checkout,name="checkout"),
我明白了AttributeError
,'Order' object has no attribute 'get_order_items'
当我尝试重定向show_order
到show_checkout
一般来说,我试图做的是为两个视图(show_checkout
,show_order
)设计网址,但这并不奏效。
order=Order.objects.get(pk=id)
问题是,变量inshow_order
和我没有包含在上面的代码中order.get_order_items(request)
的行之间存在冲突。show_checkout
所以我将变量更改order
为. 这解决了一切。诚实的错误..show_order
orders