Django - 覆盖管理员网站的登录表单 [英] Django - Override admin site's login form

查看:114
本文介绍了Django - 覆盖管理员网站的登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试覆盖Django 1.4中使用的默认格式,当登录到管理站点(我的网站使用一个额外的'令牌'字段需要用户选择进行双因素身份验证,并且是站点必须的员工)。 Django的默认表单不支持我所需要的。目前,我在我的 templates / 目录中有一个文件,名为 templates / admin / login.html 正确覆盖与我在网站其余部分使用的模板一起使用的模板。该文件的内容简单如下:

 #admin / login.html:
{%extendslogin .html%

实际登录表单如下:

 #login.html:
{%从未来添加网址%}<!DOCTYPE html>
< html>
< head>
< title>请登录< / title>
< / head>
< body>
< div id =loginform>
< form method =postaction ={%url'id.views.auth'%}>
{%csrf_token%}
< input type =hiddenname =nextvalue ={{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 =submitvalue =登录/>
< / form>
< / div>
< / body>
< / html>

我的问题是,使用正常的登录网址访问时,提供的表单完全正常,因为我提供自己的 AuthenticationForm 作为显示的表单,但通过Django Admin登录路由,Django喜欢将自己的表单提供给此模板,因此只有用户名密码字段渲染。有没有什么办法可以做这个工作,还是这个东西,我只是更好的硬编码HTML领域的形式为?

解决方案

AdminSite 有一个 login_form 属性,您可以使用它们来覆盖使用的形式。 >

子类 AdminSite 然后使用您的子类的实例,而不是 django.contrib.admin.site 在admin中注册您的模型,在urls.py等。



这些都在文档


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?

解决方案

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.

这篇关于Django - 覆盖管理员网站的登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆