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?
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.
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.
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.
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 .
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With