1

我很难弄清楚这一点,我不断收到错误消息:

show_item() takes exactly 2 arguments (1 given)

以下是我的看法:

   def show_item(request,item_slug):
    i = get_object_or_404(Item, slug=item_slug)
    categories = Category.objects.all().prefetch_related('item')
    # need to evaluate the HTTP method
    if request.method == 'POST':
        # add to order..create the bound form
        postdata = request.POST.copy()
        form = ItemAddToCartForm(request,postdata)
        # check validation of posted data
        if form.is_valid():
            # add to order and redirect to order page
            order.add_to_order(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url =urlresolvers.reverse('show_order')
            # redirect to order page
            return HttpResponseRedirect(url)
    else:
        # it's a GET, create the unbound from. Note request as a Kwarg
        form = ItemAddToCartForm(request=request,label_suffix=':')
    # assign the hidden input the item slug
    form.fields['item_slug'].widget.attrs['value'] = item_slug
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    context = {
        'categories':categories,
        'form':form,
    }
    return render_to_response("menu.html",context,context_instance=RequestContext(request))

这是我的这个视图的网址:

url(r'^menu/$','live.views.show_item'),

我环顾四周,但都是徒劳的,提前谢谢你。

4

1 回答 1

4

item slug 需要在 url 中:

url(r'^menu/(?P<item_slug>[\w-]+)$','live.views.show_item'),
于 2013-01-08T07:40:27.323 回答