1

我有四个视图从一个重定向到另一个,这就是正在做的事情,但网址不起作用。

def show_item(request,id):
        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)
            url =urlresolvers.reverse('order_index',kwargs={'id':a.id})
            # redirect to order page
            return HttpResponseRedirect(url)
        ......
    return render_to_response('item.html',context,context_instance=RequestContext(request))

上面的视图重定向到下面的这个,

def show_order(request,id):
    item = Item.objects.get(pk=order_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':
            return HttpResponseRedirect(urlresolvers.reverse('checkout',kwargs={'id':order_id}))

show_order重定向到show_checkout下面

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():
            order_created = create_order(request,resto)
            ....
            if postdata['submit'] == 'place order':
                reciept_url = urlresolvers.reverse('checkout_reciept')
                return HttpResponseRedirect(reciept_url)
    return render(request,'checkout/checkout.html',context)

网址.py,

url(r'^your_order/(\d+)/$',show_order,name="order_index"),
    url(r'^item/(?P<id>\d+)/$',show_item, name="item_order"),
    url(r'^checkout/(?P<id>\d+)/$',show_checkout,name="checkout"),

所有这一切都在第一次重定向给出错误时中断NoReverseMatch。我做得不对,我非常接受关于如何构建用于搜索场景的 url 的简要说明。

错误的完整追溯

Traceback:
File "/home/matsinvasion/projects/f4l/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/matsinvasion/projects/f4l/f4l/live/views.py" in show_item
  140.          url =urlresolvers.reverse('order_index',kwargs={'id':a.id})
File "/home/matsinvasion/projects/f4l/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
  476.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/matsinvasion/projects/f4l/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  396.                 "arguments '%s' not found." % (lookup_view_s, args, kwargs))

Exception Type: NoReverseMatch at /orders/item/3/
Exception Value: Reverse for 'order_index' with arguments '()' and keyword arguments '{'id': 3}' not found.

按照凯瑟琳的建议解决问题,导致异常 DoesNotEXist 在Show_checkout

Traceback:
File "/home/matsinvasion/projects/f4l/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/matsinvasion/projects/f4l/f4l/live/views.py" in show_checkout
  59.       resto = Restaurant.objects.get(pk=id)
File "/home/matsinvasion/projects/f4l/env/local/lib/python2.7/site-packages/django/db/models/manager.py" in get
  131.         return self.get_query_set().get(*args, **kwargs)
File "/home/matsinvasion/projects/f4l/env/local/lib/python2.7/site-packages/django/db/models/query.py" in get
  366.                     % self.model._meta.object_name)

Exception Type: DoesNotExist at /orders/checkout/3/
Exception Value: Restaurant matching query does not exist

完全的checkout_view

def show_checkout(request,id):
    """checkout form to collect order information"""
    if order.is_empty(request):
        cart_url = urlresolvers.reverse('order_index')
        return HttpResponseRedirect(cart_url)
    if request.method == 'POST':
        resto = Restaurant.objects.get(pk=id)
        postdata = request.POST.copy()
        form = forms.CheckoutForm(request.POST,postdata)
        if form.is_valid():
            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)
    else:
        form = forms.CheckoutForm
    context = {
        'form':form,
    }
    return render(request,'checkout/checkout.html',context)
4

2 回答 2

0

您定义order_id而不是id. order_id没有价值。

def show_order(request,id):
    item = Item.objects.get(pk=id)
    .......

url(r'^your_order/(?P<id>\d+)/$',show_order,name="order_index"),
url(r'^item/(?P<id>\d+)/$',show_item, name="item_order"),
url(r'^checkout/(?P<id>\d+)/$',show_checkout,name="checkout"),
于 2013-03-13T07:11:52.827 回答
0

整个问题是如何解决的..感谢凯瑟琳 show_order必须有项目 ID 而不是 order_id 即应该是

def show_order(request,id):
    item = Item.objects.get(pk=id)

并不是,

def show_order(request,id):
    item = Item.objects.get(pk=order_id)

同样在 show_checkout 我必须以不同的方式获取餐厅 ID

不是这个

def show_checkout(request,id):
    ......
    if request.method == 'POST':
        resto = Restaurant.objects.get(pk=id) 

但是这个

def show_checkout(request,id):
    .......
    if request.method == 'POST':
        item = Item.objects.get(pk=id)
        postdata = request.POST.copy()
        form = forms.CheckoutForm(request.POST,postdata)
        if form.is_valid():
            order_created = create_order(request,item)

并且在create_order

 def create_order(request,obj):
        """create new order containing each orderitem instance,save order and empty the order."""
        anon_user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
        resto = Item.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.owner
于 2013-03-13T08:46:48.300 回答