tango4567 / solutions

Here I am adding lots of different stuff. This is the collection of problems with their solutions. Most important please share good vibes and correct me if you found anything wrong here. Thanks in advance.
Apache License 2.0
5 stars 0 forks source link

NoReverseMatch Error on Django Redirect #43

Closed tango4567 closed 2 years ago

tango4567 commented 2 years ago

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 commented 2 years ago

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