4

Below is default setting in django.contrib.auth.views.LogoutViews,

template_name = 'registration/logged_out.html'

i configurate my app's urls.py like this:

from django.urls import path
from . import views

from django.conf import settings

from django.contrib.auth.views import LoginView, LogoutView

app_name = 'account'
urlpatterns = [
    #path("login/", views.user_login, name="user_login"),
    path("login/", LoginView.as_view(), name="user_login"),
    path("nlogin/", LoginView.as_view(), {"template_name":"account/login.html"}),
    path("logout/", LogoutView.as_view(), name="user_logout"),
    path("logoutt/", LogoutView.as_view(), {"template_name":"account/logout.html"}),
]

"template_name":"account/login.html" works properly, but "template_name":"account/logout.html" seems make no difference, what's wrong with my code?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Leon Li
  • 95
  • 1
  • 7

2 Answers2

5

When you use the class-based variant, you pass settings to the view through the .as_view (the so called **initkwargs) method:

from django.urls import path
from . import views

from django.conf import settings

from django.contrib.auth.views import LoginView, LogoutView

app_name = 'account'

urlpatterns = [
    #path("login/", views.user_login, name="user_login"),
    path("login/", LoginView.as_view(), name="user_login"),
    path("nlogin/", LoginView.as_view(template_name='account/login.html')),
    path("logout/", LogoutView.as_view(), name="user_logout"),
    path("logoutt/", LogoutView.as_view(template_name='account/logout.html')),
]

Otherwise the parameters will end up in the self.kwargs, and the class-based view does not inspect these.

The documentation on the LoginView [Django-doc] mentions this as well as a list of parameters you can pass as **initkwargs.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    ok, thank you a lot, i read a book in django 1.0, template_name pass in the style of url(r"^login/$", auth_views.login, {"template_name":"account/loing.html"}), but i act on django 2.1, pass settings in path, i confused by this two way, thank you a lot! – Leon Li Sep 04 '18 at 09:59
1

According to Willem Van Onsem's advice, i found the key problem is i mixed up two way of urlpatterns, like this:

  1. url() and regular expression type in urls.py (I learned in django 1.10.1 tutorial)

    from django.conf.urls import url
    from django.contrib.auth import views
    urlpatterns = [
        url(r"^login/$", views.login, {"template_name"="account/login.html"}, name='user_login'),
    ]
    
  2. path() type in urls.py(django 2.1 docs)

    from django.urls import path
    from django.contrib.auth.views import LoginView
    urlpatterns = [
        path("login/", LoginView.as_view(template_name="account/login.html"),name="user_login"),
    ]
    

It's obvious that there be two major difference to note:

  1. url import from django.conf.urls, but path import from django.urls straightly, and path type is new in django 2.0, path seems more simple
  2. in django 2.1, LoginView & LogoutView settings pass on as_view(), compare to older expression views.login, {"template_name"="account/login.html"}, simpler too
Leon Li
  • 95
  • 1
  • 7