Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Override admin site's login form

I'm currently trying to override the default form used in Django 1.4 when logging in to the admin site (my site uses an additional 'token' field required for users who opt in to Two Factor Authentication, and is mandatory for site staff). Django's default form does not support what I need. Currently, I've got a file in my templates/ directory called templates/admin/login.html, which seems to be correctly overriding the template used with the one I use throughout the rest of my site. The contents of the file are simply as below:

# admin/login.html:
{% extends "login.html" %}

The actual login form is as below:

# login.html:
{% load url from future %}<!DOCTYPE html>
<html>
    <head>
        <title>Please log in</title>
    </head>
    <body>
        <div id="loginform">
            <form method="post" action="{% url 'id.views.auth' %}">
                {% csrf_token %}
                <input type="hidden" name="next" value="{{ next }}" />
                {{ form.username.label_tag }}<br/>
                {{ form.username }}<br/>
                {{ form.password.label_tag }}<br/>
                {{ form.password }}<br/>
                {{ form.token.label_tag }}<br/>
                {{ form.token }}<br/>
                <input type="submit" value="Log In" />
           </form>
        </div>
    </body>
</html>

My issue is that the form provided works perfectly fine when accessed using my normal login URLs because I supply my own AuthenticationForm as the form to display, but through the Django Admin login route, Django likes to supply it's own form to this template and thus only the username and password fields render. Is there any way I can make this work, or is this something I am just better off 'hard coding' the HTML fields into the form for?

like image 204
TC Fox Avatar asked Jul 05 '12 09:07

TC Fox


People also ask

How do I change the admin template in Django?

To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.

Can I customize Django admin?

You can fully customize the admin by changing the templates used to render pages. The Django template engine has a defined order for loading templates. When it loads a template, it uses the first template that matches the name. You can override admin templates by using the same directory structure and file names.

How do I access my Django admin page?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).


1 Answers

This code in urls.py works fine for me (Django version 1.5.1):

from django.contrib import admin
from my.forms import AuthenticationForm

admin.autodiscover()
admin.site.login_form = AuthenticationForm
like image 159
dgk Avatar answered Sep 27 '22 17:09

dgk