How do you customize the default login form in Django?
# demo_project/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('pages.urls')),
path('users/', include('users.urls')), # new
path('users/', include('django.contrib.auth.urls')), # new
path('admin/', admin.site.urls),
]
<!-- templates/registration/login.html -->
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
Above code works but I need to customize that {{form.as_p}} Where is that form or can it be override? Thanks for the help.
Authentication Views Django provides several views that you can use for handling login, logout, and password management. These make use of the stock auth forms but you can pass in your own forms as well. Django provides no default template for the authentication views.
To create a new admin user in Django, we use the createsuperuser command, and this command can be executed using the manage.py utility. Once we execute the given command, it will prompt some fields like Username, Email, Password, Confirm Password.
Django provides no default template for the authentication views. You should create your own templates for the views you want to use. The template context is documented in each view, see All authentication views. There are different methods to implement these views in your project.
In this tutorial, we are going to build a custom user authentication system in Django. Create and activate a virtual environment to install the project dependencies: cd into the django_auth and create a users app. Add the users app to the list of installed apps in the settings.py file:
Django by default will look within a templates folder called registration for auth templates. The login template is called login.html. Create a new directory called registration and the requisite login.html file within it.
If you have the Django admin installed, you can also change user’s passwords on the authentication system’s admin pages. Django also provides views and forms that may be used to allow users to change their own passwords. Changing a user’s password will log out all their sessions.
you can overide default Authentication form
like this
from django.contrib.auth.forms import AuthenticationForm, UsernameField
from django import forms
class UserLoginForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
super(UserLoginForm, self).__init__(*args, **kwargs)
username = UsernameField(widget=forms.TextInput(
attrs={'class': 'form-control', 'placeholder': '', 'id': 'hello'}))
password = forms.CharField(widget=forms.PasswordInput(
attrs={
'class': 'form-control',
'placeholder': '',
'id': 'hi',
}
))
in url.py you can pass this custom authentication form like this
from django.contrib.auth import views
from myapp.forms import UserLoginForm
urlpatterns = [
path(
'login/',
views.LoginView.as_view(
template_name="login.html",
authentication_form=UserLoginForm
),
name='login'
)
]
also, you can override the login template
and customize the template as per you requirement
<form method="post">
{{ form.non_field_errors }}
<div class="form-group">
<label for="{{ form.username.id_for_label }}">Username:</label>
{{ form.username }}
{{ form.username.errors }}
</div>
<div class="form-group">
<label for="{{ form.password.id_for_label }}">Password:</label>
{{ form.password }}
{{ form.password.errors }}
</div>
<button type="submit">Login</button>
</form>
You can customise your form rendering by overriding/editing templates/registration/login.html as below.
This is for example only you can change css styles and formatting as you want.
<h2>Login</h2>
<form method="post">
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.username.errors }}
<label for="{{ form.username.id_for_label }}">Username:</label>
{{ form.username }}
</div>
<div class="fieldWrapper">
{{ form.password.errors }}
<label for="{{ form.password.id_for_label }}">Password:</label>
{{ form.password }}
</div>
<button type="submit">Login</button>
</form>
You may also loop over form's fields
<h2>Login</h2>
<form method="post">
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
<button type="submit">Login</button>
</form>
You could also check more form rendering options from here
From question, I assume you want to change the html template's look and feel instead of python forms class. In this case all you need to do is include input type fields with the matching names and ids attributes what default django-auth form expects. You can achieve this using following steps.
It comes something similar to below:
<form method="POST">
{% csrf_token %}
<input type="input" class="form-control" name="username" id="inputEmail" placeholder="Username" required >
<input type="password" class="form-control" name="password" id="inputPass" placeholder="Password" required>
<button type="submit" style="opacity: 1 !important;">Login</button>
<a href="/password_reset">Reset Password</a>
</form>
After this you can use your imagination and design the login form as per your requirement.
Hope this helps.
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