I’m getting a strange error with my URLconf.
I’ve got the following setup.
# root conf
urlpatterns = patterns('',
# ...
url(r'^cart/', include('bbhq.cart.urls')),
# ...
)
# bbhq.cart.urls
urlpatterns = patterns('',
# ...
url(r'^add_to_cart$', add_to_cart, name='add-to-cart-page'),
# ...
)
I have a form that uses the reverse function (successfully)
<form method="post" action="{% url add-to-cart-page %}">
However I’m getting a 404 page not found when I submit the form.
I can’t work out what’s going on here. How can the reverse function work but give me a 404 on the actual url?
I don’t know if it’s relevant but the debug info on the 404 doesn’t show the list of regex searched as it often does in these situations. This is all I get –
Page not found (404)
Request Method: POST
Request URL: http://192.168.1.98:8000/cart/add_to_cart
My view code is –
def add_to_cart(request):
if request.method == 'POST':
if 'cart' in request.session:
cart = request.session['cart']
else:
cart = Cart()
if 'item_id' not in request.POST:
raise Http404
else:
try:
item = StockItem.objects.get(pk=int(request.POST['item_id']))
except:
raise Http404
cart_add(cart, item)
request.session.modified = True
if request.is_ajax():
return render_to_response('cart/cart_summary.html',
{'cart_count': cart_items(cart),
'cart_total': cart_total(cart)},
context_instance=RequestContext(request))
else:
return HttpResponseRedirect(reverse('cart-page'))
else:
raise PermissionDenied
I’ve also tried appending a slash to the url but I get exactly the same error –
url(r'^add_to_cart/$', add_to_cart, name='add-to-cart-page'),
When you receive a standard 404 (one without the stacktrace) it usually means it executed your
raise Http404(). I suggest you use the messages and redirects instead to make things a little easier on your users as well as yourself: