我有四个视图从一个重定向到另一个,这就是正在做的事情,但网址不起作用。
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)