'Using LoginView and LogoutView with custom templates
I am trying to use the django.contrib.auth.LoginView and auth_views.LogoutView.as_view() with custom templates, without interfering with the admin site in any way, regardless the order at which the apps are loaded in the settings.py file. So far not succeeded.
This is my urls.py:
from django.urls import path, include
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
# ...
path('login/', auth_views.LoginView.as_view(), {'template_name': 'registration/login.html'}, name = 'login'),
path('logout/', auth_views.LogoutView.as_view(), {'template_name': 'registration/logged_out.html'}, name = 'logout')
]
The login view loads my custom template, but the logout does not and uses the default one. Can't figure out why, no diagnostic appears in the terminal... Please help. Thanks!

Solution 1:[1]
With class-based views with .as_view() you are instantiating an instance of a view, so changed attributes need to be passed into .as_view() method call itself.
And if you want to render view template, not redirect to LOGOUT_REDIRECT_URL - set next_page to None.
path('logout/',
auth_views.LogoutView.as_view(
template_name='registration/logged_out.html',
next_page=None
),
name = 'logout'
)
Yes, if your templates are loaded from inside each app's templates directory with
django.template.loaders.app_directories.Loader (which is also the case if settings.TEMPLATES[0]['APP_DIRS'] = True) - Django will load /search for templates inside of each app in INSTALLED_APPS, so in order to override admin templates you will need to use load them earlier in INSTALLED_APPS than django.contrib.admin.
However, this may not be always desired.
For more clarity you can use one global templates dir for your django project and apps in it and set its path in settings.TEMPLATES[0]['DIRS'].
Or, as far as you are already providing path to your custom template, use alternative name /path to avoid conflict with base templates.
Solution 2:[2]
Thats because Logoutview is not rendering template it is redirecting to another url so you have to add this.
views
class LogOutView(TemplateView):
template_name = "registration/logged_out.html"
url
path('logout-redirect/',LogoutView.as_view(), name='logout-redirect')
and then in your settings.py
LOGOUT_REDIRECT_URL = 'logout-redirect/'
Solution 3:[3]
I had the same problem once, I changed the default template name from 'logged_out.html' to 'logout.html' and passed the new template name in the LogoutView as an argument.
path(
"accounts/logout/",
auth_view.LogoutView.as_view(template_name="registration/logout.html",),
name="logout",
),
It worked in my case.
Solution 4:[4]
Check the INSTALLED_APPS Setting of your project and make sure that
django.contrib.admin
comes after your App(Account) application.
? Both templates are located in the same relative path,
? Django templates loader will use the first one it finds
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Shavzz Hussain |
| Solution 3 | Avinash Pandey |
| Solution 4 | Miraz_hossain |
