Prithviraj Mitra Prithviraj Mitra - 1 month ago 7
Python Question

NoReverseMatch error happened while redirecting to another url from in django

I am trying to redirect user after the signup is done and getting the below error.

Reverse for 'verificationsent' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] --

urlpatterns = [
url("^$", views.homepage, name="home"),
url(r'^account/', include('accounts.urls')),

accounts/ --

from django.conf.urls import url
from . import views

urlpatterns = [
url(r"^signup/$", views.signup, name="account_signup"),
url(r'^verificationsent/$', views.verificationsent, name='account_verificationsent'),
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',views.activate, name='activate'),
] --

return redirect('verificationsent')

Full traceback

Internal Server Error: /account/signup/
Traceback (most recent call last):
File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\handlers\", line 39, in inner
response = get_response(request)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\handlers\", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\handlers\", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\pyprojects\cias\ciasproj\accounts\", line 41, in signup
return redirect('verificationsent')
File "C:\virtualenvs\ciasenv\lib\site-packages\django\", line 56, in redirect
return redirect_class(resolve_url(to, *args, **kwargs))
File "C:\virtualenvs\ciasenv\lib\site-packages\django\", line 147, in resolve_url
return reverse(to, args=args, kwargs=kwargs)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\urls\", line 91, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\virtualenvs\ciasenv\lib\site-packages\django\urls\", line 392, in _reverse_with_prefix
(lookup_view_s, args, kwargs, len(patterns), patterns)
django.urls.exceptions.NoReverseMatch: Reverse for 'verificationsent' with arguments '()' and keyword arguments '{}' not
found. 0 pattern(s) tried: []
[22/Aug/2017 10:22:00] "POST /account/signup/ HTTP/1.1" 500 88145

I have followed the solutions from the below SO references but didn't work.

Redirect with parameters. NoReverseMatch at /test/

Django NoReverseMatch in form redirect

Any help is highly appreciated. Thanks in advance.

Answer Source

You should not use direct url reference statements as per best practice. Always reverse the urls

return redirect(reverse('account_verificationsent'))

If you have a namespace for your app then you have to use that in reverse also.