在 django 中捕获 NoReverseMatch 错误

发布于 2024-10-18 12:46:38 字数 1737 浏览 1 评论 0原文

我有一个名为 edit_order 的视图,还有另一个名为 client_items 的视图。

def edit_order(request, order_no)
    change_item = order.contact.client

def client_items(request, client_id = 0):
    client = None
    items = None
    try:
        client = models.Client.objects.get(pk = client_id)
        items = client.storageitem_set.all()
    except:
        return HttpResponse(reverse(return_clients))
    return render_to_response('items.html', {'items':items, 'client':client}, context_instance = RequestContext(request))

在我的编辑订单模板中,我有一个模板标签网址。

<input type="button"  value="Edit items" onclick="window.location.href='{% url tiptop.views.client_items change_item.pk  %}'" />

这有效。现在,我想创建另一个视图,它具有相同的功能,但可以使用 order_no 参数。但由于某种原因不起作用。我将此视图称为 test_items。

def test_items(request, client_id = 0, order_no=0):
    client = None
    items = None
    try:
        client = models.Client.objects.get(pk = client_id)
        items = client.storageitem_set.all()
        order = models.Order.objects.get(pk = order_no)
    except:
        return HttpResponse(reverse(return_clients))
    return render_to_response('test.html', {'items':items, 'client':client, 'order':order}, context_instance = RequestContext(request))

在我的模板中,我已将网址更改为此。

<input type="button"  value="Edit items" onclick="window.location.href='{% url tiptop.views.test_items change_item.pk  %}'" />

所以我收到这个错误。

Caught NoReverseMatch while rendering: Reverse for 'tiptop.views.test_items' with arguments '(17L,)' and keyword arguments '{}' not found.

造成这种情况的原因是 order_no 参数。但我希望能够使用这个参数。我有办法克服这个问题吗?我希望这一切都有道理。

I have a view called edit_order, and I have another view called client_items.

def edit_order(request, order_no)
    change_item = order.contact.client

def client_items(request, client_id = 0):
    client = None
    items = None
    try:
        client = models.Client.objects.get(pk = client_id)
        items = client.storageitem_set.all()
    except:
        return HttpResponse(reverse(return_clients))
    return render_to_response('items.html', {'items':items, 'client':client}, context_instance = RequestContext(request))

And in my edit order template I have a template tag url.

<input type="button"  value="Edit items" onclick="window.location.href='{% url tiptop.views.client_items change_item.pk  %}'" />

This works. Now, I want to make another view which does the same but can use an order_no parameter. But for some reason this does not work. I have called this view test_items.

def test_items(request, client_id = 0, order_no=0):
    client = None
    items = None
    try:
        client = models.Client.objects.get(pk = client_id)
        items = client.storageitem_set.all()
        order = models.Order.objects.get(pk = order_no)
    except:
        return HttpResponse(reverse(return_clients))
    return render_to_response('test.html', {'items':items, 'client':client, 'order':order}, context_instance = RequestContext(request))

And in my template I have changed the url to this.

<input type="button"  value="Edit items" onclick="window.location.href='{% url tiptop.views.test_items change_item.pk  %}'" />

So I get this error.

Caught NoReverseMatch while rendering: Reverse for 'tiptop.views.test_items' with arguments '(17L,)' and keyword arguments '{}' not found.

The reason why this is causing this is the order_no parameter. But I want to be able to use this parameter. Is there a way I can overcome this issue? I hope this all made sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡大本事 2024-10-25 12:46:38

那么,您如何传递 order_no 参数?你的 urls.py 是什么样的?在修改后的模板中,您没有将 order_no 传递给 {% url %} 标记。如果您的 URL 正则表达式需要两个参数(client_id 和 order_no),那么它将找不到匹配的 URL。您可以在 urls.py 中尝试类似的操作:

urlpatterns = patterns('tiptop.views',
    (r'^(\d+)/(\d*)

但就您而言,最好将 order_no 作为 GET 参数传递。

, 'test_items'), )

但就您而言,最好将 order_no 作为 GET 参数传递。

Well, how are you passing the order_no parameter? And what does your urls.py look like? In your modified template you are not passing an order_no to the {% url %} tag. If your URL regexp requires both parameters (client_id and order_no) then it won't find a matching URL. You could try something like this in urls.py:

urlpatterns = patterns('tiptop.views',
    (r'^(\d+)/(\d*)

But in your case, it might be better to just pass the order_no as a GET parameter.

, 'test_items'), )

But in your case, it might be better to just pass the order_no as a GET parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文