Atma
Atma

Reputation: 29767

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?

Upvotes: 7

Views: 5515

Answers (1)

Victor Castillo Torres
Victor Castillo Torres

Reputation: 10811

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!

Upvotes: 5

Related Questions