我有一個功能,我如何處理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"),
其同樣的錯誤馬朋友.. – 2013-03-08 08:51:02
我忘了一件事,我正在修正答案 – 2013-03-08 08:53:06
@SuziemacTani你有沒有試過這個? – 2013-03-08 09:04:16