Prithviraj Mitra
Prithviraj Mitra

Reputation: 11812

NoReverseMatch error happened while redirecting to another url from views.py 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: []

urls.py --

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

accounts/urls.py --

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'),
]

views.py --

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\exception.py", line 39, in inner
    response = get_response(request)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\pyprojects\cias\ciasproj\accounts\views.py", line 41, in signup
    return redirect('verificationsent')
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\shortcuts.py", line 56, in redirect
    return redirect_class(resolve_url(to, *args, **kwargs))
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\shortcuts.py", line 147, in resolve_url
    return reverse(to, args=args, kwargs=kwargs)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\urls\base.py", 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\resolvers.py", 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.

Upvotes: 1

Views: 158

Answers (1)

Arpit Solanki
Arpit Solanki

Reputation: 9931

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.

Upvotes: 1

Related Questions