Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize default auth login form in Django?

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.

like image 864
fa5pro Avatar asked Mar 27 '19 04:03

fa5pro


People also ask

What is default authentication in Django?

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.

How do I change Django default password?

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.

What is the default template for authentication views in Django?

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.

How to build a custom user authentication system in Django?

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:

How to create a login page in Django?

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.

How do I change a user password in Django?

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.


3 Answers

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>
like image 85
Nakul Narayanan Avatar answered Oct 18 '22 23:10

Nakul Narayanan


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

like image 11
Devang Padhiyar Avatar answered Oct 19 '22 00:10

Devang Padhiyar


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.

  1. Render the template as you are rendering right now (using {{form.as_p}}).
  2. Inspect elements and check user name, password and submit button's name and ids generated by default auth form.
  3. Recreate same tags using your own custom style.

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.

like image 7
SwapnilBhate Avatar answered Oct 19 '22 00:10

SwapnilBhate