i tried to add a url in django template which is supposedly to log out a user
<div><a href="{% url 'accounts.views.user_logout' %}">logout</a></div>
but it threw a NoReverseMatch
the template:
<header >
<div class='logo-text'>Nitro fleet</div>
<div >
{% if user.is_authenticated %}
<img src="{{user.picture.url}}" />
<div>
<div>{{user.username}}</div>
<div><a href="{% url 'accounts.views.user_logout' %}">logout</a></div>
</div>
{%endif%}
</div>
</header>
the url of the app (accounts) that has the logout view:
from django.urls import path
from . import views
appname = 'accounts'
urlpatterns = [
path('register', views.register_view , name='user_register'),
path('login', views.user_login , name='user_login'),
path('logout', views.user_logout , name='user_logout'),
]
the view function:
def user_logout(request):
logout(request)
return HttpResponseRedirect(request.path_info)
the complete error log:
NoReverseMatch at /maintenance/
Reverse for 'accounts.views.user_logout' not found. 'accounts.views.user_logout' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/maintenance/
Django Version: 3.2.9
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'accounts.views.user_logout' not found. 'accounts.views.user_logout' is not a valid view function or pattern name.
Exception Location: C:\Users\PC\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix
Python Executable: C:\Users\PC\AppData\Local\Programs\Python\Python310\python.exe
Python Version: 3.10.0
Python Path:
['C:\\Users\\PC\\Desktop\\nitrofleet',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python310\\DLLs',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python310\\lib',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python310',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages']
Server time: Mon, 31 Jan 2022 20:43:51 0000
CodePudding user response:
Just use the name of the view: {% url 'accounts:user_logout' %}