Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoReverseMatch Error on Django Redirect #43

Closed
tango4567 opened this issue Aug 22, 2022 · 1 comment
Closed

NoReverseMatch Error on Django Redirect #43

tango4567 opened this issue Aug 22, 2022 · 1 comment
Labels

Comments

@tango4567
Copy link
Owner

tango4567 commented Aug 22, 2022

NoReverseMatch at /register

Reverse for 'register' not found. 'register' is not a valid view function or pattern name.
Request Method: POST
http://127.0.0.1:8000/register
4.1
NoReverseMatch
Reverse for 'register' not found. 'register' is not a valid view function or pattern name.
C:\Users\TarunMahajan.virtualenvs\pracPy-b7vdO4P5\lib\site-packages\django\urls\resolvers.py, line 803, in _reverse_with_prefix
website.views.register
C:\Users\TarunMahajan.virtualenvs\pracPy-b7vdO4P5\Scripts\python.exe
3.10.4
['C:\Users\TarunMahajan\djangoProjects\pracPy', 'C:\Python310\python310.zip', 'C:\Python310\DLLs', 'C:\Python310\lib', 'C:\Python310', 'C:\Users\TarunMahajan\.virtualenvs\pracPy-b7vdO4P5', 'C:\Users\TarunMahajan\.virtualenvs\pracPy-b7vdO4P5\lib\site-packages']
Mon, 22 Aug 2022 01:45:33 +0000
Log

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/register

Django Version: 4.1
Python Version: 3.10.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback (most recent call last):
File "C:\Users\TarunMahajan\.virtualenvs\pracPy-b7vdO4P5\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\TarunMahajan\.virtualenvs\pracPy-b7vdO4P5\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\TarunMahajan\djangoProjects\pracPy\website\views.py", line 83, in register
return redirect('register')
File "C:\Users\TarunMahajan\.virtualenvs\pracPy-b7vdO4P5\lib\site-packages\django\shortcuts.py", line 48, in redirect
return redirect_class(resolve_url(to, *args, **kwargs))
File "C:\Users\TarunMahajan\.virtualenvs\pracPy-b7vdO4P5\lib\site-packages\django\shortcuts.py", line 145, in resolve_url
return reverse(to, args=args, kwargs=kwargs)
File "C:\Users\TarunMahajan\.virtualenvs\pracPy-b7vdO4P5\lib\site-packages\django\urls\base.py", line 88, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "C:\Users\TarunMahajan\.virtualenvs\pracPy-b7vdO4P5\lib\site-packages\django\urls\resolvers.py", line 803, in _reverse_with_prefix
raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /register
Exception Value: Reverse for 'register' not found. 'register' is not a valid view function or pattern name.

Here we can see register function is registered
Using the URLconf defined in tarunmahajan.urls, Django tried these URL patterns, in this order:

  1. admin/
  2. old
  3. about
  4. hello
  5. register
@tango4567
Copy link
Owner Author

tango4567 commented Aug 22, 2022

Well I used return render(request, 'register.html') instead of return redirect('register') so it's solved my problem temporary but I'm keep looking on it. Even after registering function properly in urls.py

Some interesting conversation I found mention below.

  1. NoReverseMatch error
  2. NoReverseMatch at /blog/redirect2
  3. Why am I getting no reverse match error in django 3.2?

Solution: Well I missed to mention function name in urlpatterns arrays at urls.py
from django.urls import path
from . import views

Previous code
urlpatterns = [
    path('', views.index),
    path('old', views.view),
    path('about', views.about),
    path('hello', views.say_hello),
    path('register', views.register),
    path('login', views.login),
    path('logout', views.logout)
]
Changed code
urlpatterns = [
    path('', views.index),
    path('old', views.view, name='old'),
    path('about', views.about, name='about'),
    path('hello', views.say_hello, name='say_hello'),
    path('register', views.register, name='register'),
    path('login', views.login,name='login'),
    path('logout', views.logout, name='logout')
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant