0

我有两种看法,

def create_order(request,id):
    orders = Order()
    orders.restaurant = Restaurant.objects.get(pk=id)
    orders.save()

    if orders.pk:
        ....
    return orders

第二个视图调用第一个视图,

def show_checkout(request,id):
    if order.is_empty(request):
        ...
    if request.method == 'POST':
        restaurant = Restaurant.objects.get(pk=id)
        form = forms.CheckoutForm(request.POST,instance=restaurant)
        if form.is_valid():
            order_created = create_order(request,restaurant)
            .......
    return render_to_response('checkout/checkout.html',context,context_instance=RequestContext(request))

我当我运行这个我得到错误`

show_checkout() 正好接受 2 个参数(1 个给定)` 我的 Traceback,

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)

Exception Type: TypeError at /orders/checkout/
Exception Value: show_checkout() takes exactly 2 arguments (1 given)

我会继续这样做,并建议阅读有关处理争论的好书,这是值得赞赏的

from .views import show_item, show_order,get_category,homepage,show_checkout,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/$',show_order,name="order_index"),
    url(r'^item/(?P<id>\w+)/$',show_item, name="item_order"),
    url(r'^checkout/$',show_checkout,name="checkout"),
    url(r'^reciept/$',reciept,name="checkout_reciept"),
)

修复 urls.py,NoReverseMatch error在这个函数中引发,

Reverse for 'checkout' with arguments '()' and keyword arguments '{}' not found.
    def get_checkout_url(request):
        return urlresolvers.reverse('checkout')
4

1 回答 1

1

您的show_checkout视图采用了id您未在 URL 中考虑的第二个参数。

您需要将其添加到您的 URL,如下所示:

url(r'^checkout/(?P<checkout_id>[\w|\W]+)$',show_checkout,name="checkout"),

当您在模板中调用 URL 时,您需要id在视图中传递您要求的内容,如下所示:

{% url checkout checkout.id %}

或者,如果您使用的是 Django 1.5:

{% url 'checkout' checkout.id %}
于 2013-03-07T21:31:46.377 回答