1

我有这三个功能,我把它们安排得很远

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_ordershow_checkout

一般来说,我试图做的是为两个视图(show_checkoutshow_order)设计网址,但这并不奏效。

order=Order.objects.get(pk=id)问题是,变量inshow_order和我没有包含在上面的代码中order.get_order_items(request)的行之间存在冲突。show_checkout所以我将变量更改order为. 这解决了一切。诚实的错误..show_orderorders

4

1 回答 1

1

问题是,show_order 中的变量order=Order.objects.get(pk=id)和 show_checkout 中的一行之间存在冲突order.get_order_items(request),我没有包含在上面的代码中。所以我将 show_order 中的变量顺序更改为订单。这解决了一切。老实人的错误。。

于 2013-03-13T05:49:10.790 回答