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?
Holá
I found a very simple solution.
You just need to modify the urls.py fle of the project (note, not the application one)
from your_app_name import views
url(r'^admin/', include(admin.site.urls))
url(r'^admin/login/', views.your_login_view),
This is an example
from django.conf.urls import include, url
from django.contrib import admin
from your_app import views
urlpatterns = [
url(r'^your_app_start/', include('your_app.urls',namespace="your_app_name")),
url(r'^admin/login/', views.your_app_login),
url(r'^admin/', include(admin.site.urls)),
]