这是我第一次遇到这样的错误而且我已经环顾四周但未能在相同的情况下发现错误。重定向应该发生在用户提交表单时。
在我的视图中,我使用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))
上述视图的 urls :
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.
谢谢。
答案 0 :(得分:1)
传递给reverse的值应该是url的名称而不是视图。
url =urlresolvers.reverse('order_index')