Django - Override admin site's login form

一笑奈何 提交于 2019-12-18 05:03:24

问题


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?


回答1:


AdminSite has a login_form attribute that you can use to override the form used.

Subclass AdminSite and then use an instance of your subclass instead of django.contrib.admin.site to register your models in the admin, in urls.py, etc.

It's all in the documentation.




回答2:


Holá
I found a very simple solution.
You just need to modify the urls.py fle of the project (note, not the application one)

  1. In your PROJECT folder locate the file urls.py.
  2. Add this line to the imports section
    from your_app_name import views
  3. Locate this line
    url(r'^admin/', include(admin.site.urls))
  4. Add above that line the following
    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)),
]



回答3:


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



回答4:


I know this is an old thread, but I wanted to add something it helped me find, in case it can help others.

I'm using a special remote auth (Shibboleth) and overriding the admin login_form wouldn't be enough. I have a view that sets cookies and return variables and redirects to the remote auth provider, etc.

So the way I did it was:

from my_app.views import user_login, user_logout
admin.autodiscover()
admin.site.login = user_login
admin.site.logout = user_logout

Works great! Thanks to @dgk




回答5:


Maybe use a templatetag to obtain your custom AuthenticationForm?



来源:https://stackoverflow.com/questions/11341663/django-override-admin-sites-login-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!