Add More field in django admin login page.

i want to add one more field above let say tenant_identidier.
is it possible?

Absolutely.

The template is located at django.contrib.admin.templates.admin.login.html. It’s defined in the AdminSite class by the name login_template. Then there’s the login_form that you would override to define that field.

Finally, depending upon what it is that you’re trying to do with this additional field, you may need to override the AdminSite.login method to use your desired LoginView class.

See Customizing the AdminSite class for more information. I also suggest you read the source code for the AdminSite class, especially the login method, to understand how the existing process works before modifying or extending it.

do i need to create my custom login page ?

What do you mean by “login page” here?

All those components I mentioned above (the template, form, and view) are used to create a page. You need to do all of that.

i’ve extended the authentication form and added one more field. as u can see in the below image.


but when i am trying to login it is not allowing me .it is throwing error.

Please post all the changes you’ve made to do this. This includes your new template, form, LoginView, AdminSite, admin.py, apps.py, and settings.py.

login.html

{# myapp/templates/admin/login.html #}
{% extends 'admin/login.html' %}

{% block content %}
    {{ block.super }}
    {{ form.tenant_identifier.label_tag }} {{ form.tenant_identifier }}
    
    {% if form.errors %}
        <p class="error">Please correct the error below:</p>
        {{ form.errors }}
    {% endif %}
{% endblock %}

forms.py

# forms.py

from django import forms
from django.contrib.auth.forms import AuthenticationForm

class CustomAuthenticationForm(AuthenticationForm):
    tenant_identifier = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Enter your tenant identifier'}))

views.py

# views.py

from django.shortcuts import render, redirect
from django.contrib.auth.views import LoginView
from .forms import CustomAuthenticationForm

class CustomLoginView(LoginView):
    # template_name = 'login.html'
    authentication_form = CustomAuthenticationForm

    def form_valid(self, form):
        # Add your authentication logic here
        return super().form_valid(form)

urls.py

from django.contrib import admin
from django.urls import path

from myapp.views import CustomLoginView
# from myapp.admin import custom_admin_site


urlpatterns = [
    # path('admin/login/', CustomLoginView.as_view(), name='login'),
    path('admin/', admin.site.urls),
   path('login/', CustomLoginView.as_view(), name='login'),
    # other URL patterns...
]

getting this error.

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code (or template), then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of updating your post for this while I’m reviewing it.)

Couple things:

  • You probably want to have your form inherit from AdminAuthenticationForm instead of AuthenticationForm. (It’s not the problem here, but it is something you’ll need to fix.)

  • Your template is using block.super to render the content block from the parent template. However, that parent block contains the complete form tag, including the </form> end tag. This causes a problem because this means that the form.tenant_identifier field is being rendered outside the form, and so it’s not going to be submitted to the server.


{% block content %}
    <div class="custom-form">
        <form method="post" action="{% url 'admin:login' %}">
            {% csrf_token %}
            {{ block.super }} {# Include the entire form block from the parent template #}
            
            {{ form.tenant_identifier.label_tag }} {{ form.tenant_identifier }}
            
            {% if form.errors %}
                <p class="error">Please correct the error below:</p>
                {{ form.errors }}
            {% endif %}
        </form>
    </div>
{% endblock %}

is that correct way?

Take a look at what this renders and see if it’s valid.