Django - Override admin site's login form

Posted by TrojanCentaur on Stack Overflow See other posts from Stack Overflow or by TrojanCentaur
Published on 2012-07-05T09:39:08Z Indexed on 2012/07/05 15:16 UTC
Read the original article Hit count: 396

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?

© Stack Overflow or respective owner

Related posts about django

Related posts about django-admin