简体   繁体   中英

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM