2

我的 URLconf 出现一个奇怪的错误。

我有以下设置。

# 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'),
    # ...
)

我有一个使用反向功能的表格(成功)

<form method="post" action="{% url add-to-cart-page %}">

但是,当我提交表单时,找不到 404 页面。

我无法弄清楚这里发生了什么。反向功能如何工作,但在实际网址上给我一个 404?

我不知道它是否相关,但 404 上的调试信息并没有像在这些情况下经常那样显示搜索的正则表达式列表。这就是我所得到的——

Page not found (404)
Request Method: POST
Request URL:    http://192.168.1.98:8000/cart/add_to_cart

我的视图代码是 -

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

我也尝试在 url 上附加一个斜杠,但我得到了完全相同的错误 -

url(r'^add_to_cart/$', add_to_cart, name='add-to-cart-page'),
4

1 回答 1

1

当您收到标准 404(没有堆栈跟踪的)通常意味着它执行了您的raise Http404(). 我建议您使用消息重定向来使您的用户和您自己的事情变得更容易:

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:
            '''
            HERE BE CHANGES
            '''
            # The id was not supplied
            messages.error(request, 'No item was provided')
            return HttpResponseRedirect(request.META['HTTP_REFERER'])
        else:
            try:
                item = StockItem.objects.get(pk=int(request.POST['item_id']))
            except:
                '''
                HERE BE CHANGES
                '''
                # The item was not found in the database
                messages.error(request, 'Unable to find the specified item.'
                return HttpResponseRedirect(request.META['HTTP_REFERER'])
            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
于 2012-12-11T18:50:03.863 回答