0

我有一个功能,

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"),
4

3 回答 3

0

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

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

HttpResponseRedirect(urlresolvers.reverse('checkout',kwargs={'id':your_id}))
于 2013-03-08T08:46:55.953 回答
0

你应该使用而resto.id不是仅仅喜欢restoreverse()

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

代替

return HttpResponseRedirect(urlresolvers.reverse('checkout',kwargs={'id':resto}))
于 2013-03-08T09:21:05.493 回答
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}))
于 2013-03-08T09:15:12.700 回答