Search code examples
djangodjango-formsdjango-authentication

Overriding LoginView has no effect in the registration template


I tried to add some classes to my projects login template via overriding the LoginView as described here, but I seem to miss something obvious:

views.py:

class CustomLoginView(LoginView):
    # template_name = "registration/login.html" # standard
    form = UserLoginForm

forms.py:

class UserLoginForm(AuthenticationForm):
    username = forms.CharField(label = "", widget = forms.TextInput(attrs = {"class": "test", "placeholder": "username or email",}))
    password = forms.CharField(label = "", widget = forms.PasswordInput(attrs = {"placeholder": "",}))

project.urls.py:

from myapp.views import CustomLoginView

urlpatterns = [
    path("home/", RedirectView.as_view(url = "/")),
    path("admin/", admin.site.urls),
    path("", include("myapp.urls")),
    # path("", include("django.contrib.auth.urls")),
    path("login/", CustomLoginView.as_view(), name = "login"),
]

login.html:

<form action="" method="post" novalidate>
    {% csrf_token %}
    {{ form }}
    <button type="submit" value="login">login</button>
</form>

But when I open the login/ url I can't see the placeholder or the class I added, which makes me think, that my customized form is not being used at all - I just don't understand why. I tried excluding path("", include("django.contrib.auth.urls")) in the urls.py but that had no effect. Login works fine by the way.


Solution

  • Change form = ... to form_class = ... inside of your view.

    This should work:

    class CustomLoginView(LoginView):
        # template_name = "registration/login.html" # standard
        form_class = UserLoginForm