2013-01-17 51 views
0

我所遇到的一個NoReverseMatch錯誤 Reverse for 'item_order' with arguments '()' and keyword arguments '{}' not found. 我有這種說法被稱爲當「item_order」與參數「()」和關鍵字參數模板運行,如何應對NoReverseMatch錯誤,反向爲「{}」未找到

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

你需要傳遞的id值,因爲您在URL

{% url item_order item.id %} 
+0

哎charl的指定,由於是工作,但到七次分鐘未完成的,我不能接受的答案。 –

+0

沒問題,很高興幫助:) – Charl

+0

如果你不介意,我有這個讓我頭痛的其他錯誤,我認爲它更多的是在視圖中使用的邏輯。你不介意檢查出來 –

相關問題