Getting a Caught NoReverseMatch error in django

2019-09-01 16:28发布

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.

1条回答
Animai°情兽
2楼-- · 2019-09-01 16:54

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

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

查看更多
登录 后发表回答