我如何处理NoReverseMatch?

时间:2013-03-08 08:41:09

标签: django django-urls

我有一个功能,

def show_checkout(request,id):
    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)
        form = forms.CheckoutForm(request.POST,instance=restaurant)
        if form.is_valid():
            order_created = create_order(request,resto)
            .....
    return render(request,'checkout/checkout.html',context)

在下一个视图中,我尝试提交表单,

def show_order(request):
    if request.method == 'POST':
        postdata = request.POST.copy()
        if postdata['submit'] == 'checkout':
            checkout_url = checkout.get_checkout_url(request)
            return HttpResponseRedirect(checkout_url)

上面的视图在checkout.py中调用此视图

def get_checkout_url(request):
    return urlresolvers.reverse('checkout')

urls.py url(r'^checkout/(?P<id>\w+)$',show_checkout,name="checkout"),

毕竟这个我得到错误, Reverse for 'checkout' with arguments '()' and keyword arguments '{}' not found. 我以为我是在正确的轨道上,我想我不是。

编辑 在show_order我做

def show_order(request,id):
    resto = Restaurant.objects.get(pk=id)
    ...
        if postdata['submit'] == 'checkout':
#           checkout_url = checkout.get_checkout_url(request)
            return HttpResponseRedirect(urlresolvers.reverse('checkout',kwargs={'id':resto}))

在网址中我做url(r'^your_order/(?P<id>\w+)$',show_order,name="order_index"), 引发错误page not found

完成urls.py

from .views import show_item,show_checkout,show_order,get_category,homepage,reciept
from f4l import settings
from django.conf.urls.defaults import patterns, include, url


urlpatterns = patterns('',
    url(r'^$',homepage,name="homepage"),
    url(r'^menu/(\d+)$',get_category, name="f4l_menu"),
    url(r'^your_order/(?P<id>\w+)$',show_order,name="order_index"),
    url(r'^item/(?P<id>\w+)/$',show_item, name="item_order"),
    url(r'^checkout/(?P<id>\w+)$',show_checkout,name="checkout"),
    url(r'^reciept/$',reciept,name="checkout_reciept"),

3 个答案:

答案 0 :(得分:0)

您不需要此get_checkout_url函数,需要传递url的id参数。

而不是return HttpResponseRedirect(checkout_url)试试这个:

HttpResponseRedirect(urlresolvers.reverse('checkout',kwargs={'id':your_id}))

答案 1 :(得分:0)

from django.conf.urls.defaults import patterns, include, url

urlpatterns = patterns('live.views',
    url(r'^$', 'homepage', name="homepage"),
    url(r'^menu/(\d+)/$', 'get_category', name="f4l_menu"),
    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"),
    url(r'^reciept/$', 'reciept', name="checkout_reciept"),


def show_order(request,id):
    resto = Restaurant.objects.get(pk=id)
    ...
        if postdata['submit'] == 'checkout':
            return HttpResponseRedirect(reverse('live:order_index',kwargs={
                'id':id}))

答案 2 :(得分:0)

您应该resto.id使用resto而不仅仅是reverse()

return HttpResponseRedirect(urlresolvers.reverse('checkout',kwargs={'id':resto.id}))

而不是

return HttpResponseRedirect(urlresolvers.reverse('checkout',kwargs={'id':resto}))