# Placeholders for Login

**URL:** <https://forum.djangoproject.com/t/placeholders-for-login/40955>\
**Category:** Using Django\
**Created:** [May 16, 2025, 9:37am UTC](https://forum.djangoproject.com/t/placeholders-for-login/40955 "2025-05-16T09:37:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tahaqaiser313](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tahaqaiser313/32/27443_2.png) [@tahaqaiser313](https://forum.djangoproject.com/u/tahaqaiser313)\
**Post date:** [May 16, 2025, 9:37am UTC](https://forum.djangoproject.com/t/placeholders-for-login/40955/1 "2025-05-16T09:37:12Z")

</div>

```auto

from . import views
from django.urls import path
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('', views.homepage, name='homepage'),
    path('register/', views.register, name="register"),
    path('login/',auth_views.LoginView.as_view(template_name="login.html"), name="login"),
    path('logout/', auth_views.LogoutView.as_view(next_page="homepage"), name="logout"),
]

```

I have a Login Form, considering I used the usercreationform, and the login form is built in, how would I add a placeholder for both the username and password?

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [May 16, 2025, 10:55am UTC](https://forum.djangoproject.com/t/placeholders-for-login/40955/2 "2025-05-16T10:55:07Z")

</div>

The same way I would do with any other form.

```auto
from django import forms

class SomeForm(forms.Form):
  the_field = forms.CharField(widget=forms.widgets.TextInput(attrs={"placeholder": "my placeholder"}))

  # Or... on the init method
  def __init__ (self, *args, **kwargs):
    super(). __init__ (*args, **kwargs)
    self.fields["the_field"].widget.attrs["placeholder"] = "my placeholder"

```

The key here is to find where the `form` is defined. I suggest that you look into the `auth_views.LoginView` definition, on most IDEs this is the `F12` or `Ctrl + Click`. This will lead you to the following code.

> <https://github.com/django/django/blob/994dc6d8a1bae717baa236b65e11cf91ce181c53/django/contrib/auth/views.py#L66>

Notice that the `LoginView` that you’re using, have both interesting attributes, `form_class` or `authentication_form`, I will use `form_class` on this example.  
`form_class` is defined as `AuthenticationForm` and if you go to the definition, then you’ll find that this forms is defined as.

> <https://github.com/django/django/blob/994dc6d8a1bae717baa236b65e11cf91ce181c53/django/contrib/auth/forms.py#L302>

Notice that it has a `username` field. Let’s say I want to add a placeholder to this field, then I can use one of the methods that I’ve shown before. In this case, it’s easier to override the ` __init__ ` method, because we only want to override a single attribute. So I will define on a `forms.py` (or in the same directory, up to you) the new form, that will inherit from the `AuthenticationForm`

```auto
# forms.py
from django.contrib.auth import forms as auth_forms

class MyAuthenticationForm(auth_forms.AuthenticationForm):
  def __init__ (self, *args, **kwargs):
    super(). __init__ (*args, **kwargs)
    self.fields["username"].widget.attrs["placeholder"] = "The placeholder"

```

Now we need to use tell the view to use this form. You have 2 options, you can:

1. do a similar “form inheritance” approach with the view and override the `form_class` attribute on your view;
2. pass the `form_class` on the `as_view` method

I will use the 2nd one, since it’s the shortest. Change this line:

> [@tahaqaiser313](#):
>
> ```auto
> path('login/',auth_views.LoginView.as_view(template_name="login.html"), name="login"),
> 
> ```

With this:

```auto
from .forms import MyAuthenticationForm # This assumes that you've created a forms.py before

urlpatterns = [
  # .... others remain the same
  path('login/',auth_views.LoginView.as_view(template_name="login.html", form_class=MyAuthenticationForm), name="login"),
]

```

Hope that helps.
