在django中捕获NoReverseMatch错误 [英] Getting a Caught NoReverseMatch error in django

查看:163
本文介绍了在django中捕获NoReverseMatch错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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))

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

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  %}'" />

这个工作。现在,我想做另一个观点,但是可以使用一个order_no参数。但是由于某种原因这个不起作用。我已经调用了这个视图test_items。

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))

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

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  %}'" />

所以我收到这个错误。

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

导致此问题的原因是order_no参数。但是我想要使用这个参数。有没有办法可以克服这个问题?我希望这一切都有道理。

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.

推荐答案

嗯,你如何传递order_no参数?你的urls.py是什么样子的?在您修改的模板中,您没有将order_no传递给{%url%}标记。如果您的URL regexp需要两个参数(client_id和order_no),那么它将找不到匹配的URL。你可以在urls.py中尝试这样的东西:

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*)$', 'test_items'),
)

但在你的情况下,最好只是将order_no作为GET参数传递。

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

这篇关于在django中捕获NoReverseMatch错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆