2013-01-10 125 views
0

它是我第一次遇到這樣的錯誤,我環顧四周,但未能在相同的情況下發現錯誤。重定向應該發生在用戶提交表單時發生。如何處理NoReverseMatch錯誤?

在我的看法中,我使用reverse()函數返回url並重定向到它。 下面是是觀點:

def get_category(request): 
    categories = Category.objects.all().prefetch_related('item') 
    form = ItemAddToCartForm(request=request, label_suffix=':') 
    # 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 
    # 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('category.html',context,context_instance=RequestContext(request)) 

這裏是URL的視圖它定向到:

def show_order(request): 
    if request.method == 'POST': 
     if postdata['submit'] == 'Remove': 
      order.remove_from_order(request) 
     if postdata['submit'] == 'Update': 
      order.update_order(request) 
    order_items = order.get_order_items(request) 
    page_title = 'F4L order' 
    order_subtotal = order.order_subtotal(request) 
    return render_to_response('public/order.html',context_instance=RequestContext(request)) 

網址爲上述觀點:

from django.conf.urls.defaults import * 

urlpatterns = patterns(
    'live.views', 
    url(r'^$', view="show_order",name="order_index"), 
) 

網址:

url(r'^orders/', include('live.urls')), 
url(r'^menu/$','live.views.get_category'), 

這裏是HTML表單:

<form method="post" action="."> 
    <a href="{% url order_index %}"> 
    {% csrf_token %} 
    {{ form.as_p }} 
    <br /> 
    <input type="submit" value="Add To Order" name="submit"> 
    </a> 

Traceback: 
File "/home/mats-invasion/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) 
File "/home/mats-invasion/projects/f4l/f4l/live/views.py" in get_category 
    39.   url =urlresolvers.reverse('show_order') 
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse 
    476.  return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) 
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix 
    396.     "arguments '%s' not found." % (lookup_view_s, args, kwargs)) 

Exception Type: NoReverseMatch at /menu/ 
Exception Value: Reverse for 'show_order' with arguments '()' and keyword arguments '{}' not found. 

謝謝。

回答

1

傳遞給reverse的值應該是url的名稱而不是視圖。

url =urlresolvers.reverse('order_index') 
+0

感謝charl,但隨後出現另一個問題,就像我按任何按鈕(填寫表單)重定向頁面。你有沒有遇到過這個。 –

+0

表單中設置爲提交類型的任何按鈕(type =「submit」)將提交表單。不知道我是否正確理解這一點。 – Charl

+0

不用擔心charl,我解決了問題,這是我的html。我把錯誤的地方放在了重定向URL '。 –