0

我遇到了 NoReverseMatch 错误 Reverse for 'item_order' with arguments '()' and keyword arguments '{}' not found. 我有这个视图,它在模板运行时被调用,

def show_item(request,id):
    # need to evaluate the HTTP method
    if request.method == 'POST':
        a = Item.objects.get(pk=id)
        form = forms.PartialOrderItemForm(request.POST,instance=a)
        # check validation of posted data
        if form.is_valid():

            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('order_index')
            # redirect to order page
            return HttpResponseRedirect(url)
    else:
        # it's a GET, create the unbound from. Note request as a Kwarg
        form = forms.PartialOrderItemForm(request.GET)
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    context={
#       'categories':categories,
        'form':form,
#       'menu':menu,

    }
    return render_to_response('item.html',context,context_instance=RequestContext(request))

当这个视图运行时,在某个时候,它会调用这个函数,

def add_to_order(request):  
#   import pdb; pdb.set_trace()
    postdata = request.POST.copy()
    # get item slug from post data, return blank if empty
#   item_slug = postdata.get('item_slug','')
    #get quantity added, return 0 if empty
    quantity = postdata.get('quantity',0)
    # fetch the item or return  missing page error_message
    i = get_object_or_404(Item)
    # get items in order
    order_items = get_order_items(request)
    item_in_orders = False
    # check to see if item is already in order
    for order_item in order_items:
        if order_item.item.id == i.id:
            #update the quantity if found
            order_item.augment_quantity(quantity)
            item_in_orders = True
    if not item_in_orders:
        # creat and save a new order item
        oi = OrderItem()
        oi.order_id = _order_id(request)
        oi.quantity = quantity
        oi.item = i
        oi.save()

urls.py,

from .views import show_item, show_order,get_category
from django.conf.urls.defaults import patterns, include, url


urlpatterns = patterns('',
    url(r'^$',get_category, name="item_order_home"),
    url(r'^your_order/$',show_order,name="order_index"),
    url(r'^item/(?P<id>\w+)/$', show_item, name = "item_order"),
#   url(r'^menu/$',get_category,name = "f4l_menu"),
)

在模板中,这是在做什么

{% for category in categories %}
 <h5> {{ category.title }} </h5>
  {% for item in category.item.all %}
   <li><a href="{% url item_order %}">{{item.name}}<span> {{item.price}}frw</span></li><a/>
  {% endfor %}
{% endfor %}

我想要做的是,它们是几个项目,当我点击一个时,它会将我重定向到一个表单,我在其中提交我想购买的项目的数量,当我提交表单时,我会重定向到订单页面. 但相反,我遇到了这个错误。

4

1 回答 1

4

您需要传递 id 值,因为您在 url 中指定了它

{% url item_order item.id %}
于 2013-01-17T07:52:35.987 回答