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!

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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With