我如何处理属性错误?

时间:2013-03-12 11:18:09

标签: django django-urls

我有这三个功能,我已将它们排列在他们的

之外
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)中的变量show_orderorder.get_order_items(request)show_checkout中的行order之间存在冲突,我在上面的代码中没有这样做。所以我将show_order中的变量orders更改为{{1}}。修复了一切。诚实的错误..

1 个答案:

答案 0 :(得分:1)

问题是,show_order中的变量order=Order.objects.get(pk=id)与show_checkout中的行order.get_order_items(request)之间存在冲突,我在上面的代码中未包含该冲突。所以我将show_order中的变量顺序更改为订单。修复了一切。诚实的错误..