Redirection with parameters. NoReverseMatch at / test /

views.py:

def demo(request, **kwargs): print response ...... def test(request): ...... kwargs = {'response': response} return redirect('demo', **kwargs) 

urls.py:

 from django.conf.urls import patterns, url from django.contrib.sitemaps.views import sitemap urlpatterns = patterns('clients.views', url(r'^test/', 'test', name='test'), url(r'^demo/', 'demo', name='demo'), ) 

Why do I have this error:

NoReverseMatch at / test /

Reverse for 'demo' with arguments '()' and keywords '{' response ': {u'status': u'ok '}}' not found.

Request Method: POST request URL: http://127.0.0.1: 8000 / test /

+4
source share
4 answers

When using the redirect() shortcut, you are actually doing HttpResponseRedirect() and therefore should not include the response in your kwargs .

In addition, if you want to redirect the arguments with keywords, then the call will

 redirect('/myurl/', momma="im comin' home") 

or

 redirect('/myurl/', kwargs={'loads_a_kwargs':'cowboy'}) 

The error you get is because your regexp url(r'^demo/', 'demo', name='demo') does not accept any parameters. Also, usually you should end all of your url regular expressions with $ to indicate that the capture should stop.

+4
source

This error was simply raised not from your test view, but from your demo view. According to the url callback. The demo url must match the parameters of the demo function.

for example: url: demo/ should be demo/<response>

And in case you do not want to change the url template, make your answer as GET for the demo parameter.

0
source

When you say redirect('demo', **kwargs) , internally you try to find urlpattern demo/(?P<response>\d+) . In fact, it can be either \d+ , or \w+ or something else. But you do not have this urlpattern, and therefore its failure.

So, it will pass if you define such a url pattern. But another problem with your code is that the response in kwargs is a dictionary, and you cannot grab the dictionary in the url template.

Any specific reason why you want to redirect the demo along with the status code?

0
source

NoReverseMatch Exception Occurs when the corresponding URL of your URL cannot be identified based on the parameters you specified. See Django Docs https://docs.djangoproject.com/en/dev/ref/exceptions/#noreversematch

I look at your url.py you have not included $

url(r'^test/$', 'test', name='test'),

0
source

Source: https://habr.com/ru/post/1492232/


All Articles