2013-03-08 42 views
0

我有一個功能,我如何處理NoReverseMatch?

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. 我以爲我是在正確的軌道上,我想我沒有。

編輯 在我做

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中我做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"), 

回答

0

你不需要這個get_checkout_url功能需要傳遞url的id參數。

代替return HttpResponseRedirect(checkout_url)試試這個:

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

其同樣的錯誤馬朋友.. – 2013-03-08 08:51:02

+0

我忘了一件事,我正在修正答案 – 2013-03-08 08:53:06

+0

@SuziemacTani你有沒有試過這個? – 2013-03-08 09:04:16

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})) 
+0

謝謝,但這仍然引發網頁未找到,網址(r'^ your_order /(?P \ w +)$',show_order,name =「order_index」),' – 2013-03-08 09:27:34

+0

沒有改變凱瑟琳, – 2013-03-08 09:32:37

+0

我剛剛這樣做.. – 2013-03-08 09:38:57

0

你應該使用resto.id,而不是僅僅在restoreverse()

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

,而不是

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

這仍然引發頁面不存在錯誤, – 2013-03-08 09:26:54

+0

我的網址'url(r'^ your_order /(?P \ w +)$',show_order ,name =「order_index」),這可能是問題所在 – 2013-03-08 09:28:46