Django-2-by-Example: Reverse for 'register' not found. 'register' is not a valid view function or pattern name?
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/account/login/
Django Version: 2.0.6
Python Version: 3.6.4
Installed Applications:
['account',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
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']
Template error:
In template /home/fc/Documents/1111/bookmarks/account/templates/base.html, error at line 13
Reverse for 'register' not found. 'register' is not a valid view function or pattern name.
3 : <html>
4 : <head>
5 : <title>{% block title %}{% endblock %}</title>
6 : <link href="{% static "css/base.css" %}" rel="stylesheet">
7 : </head>
8 : <body>
9 : <div id="header">
10 : <span class="logo">Bookmarks</span>
11 : {% if request.user.is_authenticated %}
12 : <ul class="menu">
13 : <l i {% if section == " dashboard" %}class="selected"{% endif %}>
14 : <a href="{% url "dashboard" %}">My dashboard</a>
15 : </li>
16 : <li {% if section == "images" %}class="selected"{% endif %}>
17 : <a href="#">Images</a>
18 : </li>
19 : <li {% if section == "people" %}class="selected"{% endif %}>
20 : <a href="#">People</a>
21 : </li>
22 : </ul>
23 : {% endif %}
Traceback:
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
158. response = self.process_exception_by_middleware(e, request)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
156. response = response.render()
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/response.py" in render
106. self.content = self.rendered_content
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/response.py" in rendered_content
83. content = template.render(context, self._request)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/backends/django.py" in render
61. return self.template.render(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/base.py" in render
175. return self._render(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/base.py" in _render
167. return self.nodelist.render(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/base.py" in render
943. bit = node.render_annotated(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910. return self.render(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/loader_tags.py" in render
155. return compiled_parent._render(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/base.py" in _render
167. return self.nodelist.render(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/base.py" in render
943. bit = node.render_annotated(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910. return self.render(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/loader_tags.py" in render
67. result = block.nodelist.render(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/base.py" in render
943. bit = node.render_annotated(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910. return self.render(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/defaulttags.py" in render
314. return nodelist.render(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/base.py" in render
943. bit = node.render_annotated(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/base.py" in render_annotated
910. return self.render(context)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/template/defaulttags.py" in render
447. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/urls/base.py" in reverse
90. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/fc/.local/share/virtualenvs/1111-oxI__AFh/lib/python3.6/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
636. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /account/login/
Exception Value: Reverse for 'register' not found. 'register' is not a valid view function or pattern name.
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth import authenticate, login
from .form import LoginForm
from django.contrib.auth.decorators import login_required
# Create your views here.
def user_login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
user = authenticate(request,
username=cd['username'],
password=cd['password'])
if user is not None:
if user.is_active:
login(request, user)
return HttpResponse('Authenticated '\
'successfully')
else:
return HttpResponse('Disabled account')
else:
return HttpResponse('Invalid login')
else:
form = LoginForm()
return render(request, 'account/login.html', {'form': form})
@login_required
def dashboard(request):
return render(request, 'account/dashboard.html', {'section': 'dashboard'})
urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns =[
# path('login/', views.user_login, name='login')
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('', views.dashboard, name='dashboard'),
]
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 22 (1 by maintainers)
Hi fangchaooo, Please follow all the instructions mentioned in the chapter and refer to the code files provided with the book. You should not face any difficulty.
@Jeswin17 yes I did
this how i resolve the issue,
in urls.py if you use:
path('login/', auth_views.LoginView.as_view(), name='login'),then you also need to usepath('register/', user_views.register, name='register'),somehow this solve the issue.
NameError: name ‘user_views’ is not defined
tell how please
okay , I had the same issue too while trying to view my home page. the problem is likely from your urls.py .
from django.urls import path from . import views
urlpatterns = [ path(‘’, views.HomePageView.as_view(), name = ‘home’) ] make sure to specify the name of the view. hope this helps
see this error : In template /home/fc/Documents/1111/bookmarks/account/templates/base.html, error at line 13
The reason is, you have to specify the url literal along with the ‘app_name’ of your project .
Ex:Use app_name in urls.py file and use this app_name along with the view name specified in urls.py to load the url in template [ href = " url ‘app_name:dashboard’ %} ]
Note : colon[ : ] should be specified in between ‘app_name’ and ‘name specified in urls.py’ file
In urls.py
from django.urls import path from django.contrib.auth import views as auth_views from . import views
app_name = ‘demo’. # Add this to your urls.py file according to the app name that you created for your project urlpatterns =[ # path(‘login/’, views.user_login, name=‘login’) path(‘login/’, auth_views.LoginView.as_view(), name=‘login’), path(‘logout/’, auth_views.LogoutView.as_view(), name=‘logout’), path(‘’, views.dashboard, name=‘dashboard’), ]
Then you have to use
14 : <a href=“{% url “demo:dashboard” %}”>My dashboard. # add this in your templates file so as to load url
The reason behind the usage of ‘app_name’ is, django doesn’t know which app url literal to load as you have multiple apps in your project . So, it’s better specify ‘app_name’ for every urls.py file in an app of a project
Trust me ! It works a lot better than before
Remove href to {% url “register” %} from login.html on this examples step. There was your copy-past result probably.
Do you have any idea why that error occured? And any idea how to trobubleshoot that issue cause i am having the same problem.