Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do a reverse on a url with primary key in django

I have the following url entry for my app:

url(r'^(?P<pk>\d+)/(?P<action>add|edit)/type/$', 'customer.views.edit', name='customer-edit'),

I want to post to this url with a reverse. When I do the following I get the error NoReverseMatch:

self.client.post(reverse('customer-edit'), {'something:'something'}, follow=True)

This is the full error: NoReverseMatch: Reverse for 'customer-edit' with arguments '()' and keyword arguments '{}' not found.

Do I need to pass args or kwargs to the reverse? If so what would they look like to match the url above?

like image 751
Atma Avatar asked Jul 17 '13 02:07

Atma


People also ask

What is URL reverse in Django?

the reverse function allows to retrieve url details from url's.py file through the name value provided there. This is the major use of reverse function in Django. Syntax: Web development, programming languages, Software testing & others. from django.urls import reverse.

How can I get previous URL in Django?

You can do that by using request. META['HTTP_REFERER'] , but it will exist if only your tab previous page was from your website, else there will be no HTTP_REFERER in META dict . So be careful and make sure that you are using . get() notation instead.

What is reverse and reverse lazy in Django?

Reverse_lazy is, as the name implies, a lazy implementation of the reverse URL resolver. Unlike the traditional reverse function, reverse_lazy won't execute until the value is needed. It is useful because it prevent Reverse Not Found exceptions when working with URLs that may not be immediately known.

How do I name a URL in Django?

Django offers a way to name urls so it's easy to reference them in view methods and templates. The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py .


1 Answers

To pass args to an url you can pass a variable named args of type tuple to reverse:

self.client.post(reverse('customer-edit', args=(1, 'add',)), {'something:'something'}, follow=True)

another problem is that the dict has a Syntax Error

self.client.post(reverse('customer-edit', args=(1, 'add',)), {'something' : 'something'}, follow=True)

Hope this helps you!

like image 54
Victor Castillo Torres Avatar answered Sep 29 '22 02:09

Victor Castillo Torres