如何使用带有可选参数的 Django 的 Reverse 来获取信息?我继续得到
View .py:
def cartForm(request, prod):
if request.method=="POST":
quantity = request.POST.get('quantity', False)
if quantity:
add_to_cart(request, prod, quantity)
return HttpResponseRedirect(reverse("cart"))
#if no quantity indicated, display error message
return HttpResponseRedirect(reverse('products.views.info', kwargs={'prod': prod, 'error':True}))
def info(request, prod, error=False):
prod = Product.objects.get(id=prod)
return render(request, "products/info.html", dict(product = prod, error=error))
网址.py:
url(r'^(?P<prod>\d+)/', "products.views.info", name='info'),
我不断收到以下错误:
Reverse for 'products.views.info' with arguments '()' and keyword arguments '{'prod': u'2', 'error': True}' not found. 1 pattern(s) tried: ['products/(?P<prod>\\d+)/']
最佳答案
不是直接的答案,而是:为什么不使用 Messages 框架( https://docs.djangoproject.com/en/1.6/ref/contrib/messages/ )。
关于python - 带有可选参数的 Django HTTPResponseRedirect & Reverse,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21112775/