英文:
How to override the templates of `django-two-factor-auth` for Django Admin?
问题
- 我试图覆盖Django Admin的django-two-factor-auth模板,但我不知道如何做。 *我没有使用Django的前端,而是使用
Next.js
作为前端,Django作为后端。
这是我的Django项目:
django-project
|-core
| |-settings.py
| └-urls.py
|-my_app1
| |-models.py
| |-admin.py
| └-urls.py
└-templates
而且,我按照文档的说明设置了django-two-factor-auth
,首先,我安装了django-two-factor-auth[phonenumbers]
:
pip install django-two-factor-auth[phonenumbers]
然后,如下所示,在core/settings.py
中将以下应用程序添加到INSTALLED_APPS中,将OTPMiddleware
添加到MIDDLEWARE中,设置LOGIN_URL和LOGIN_REDIRECT_URL:
# "core/settings.py"
INSTALLED_APPS = [
...
'django_otp', # 这里
'django_otp.plugins.otp_static', # 这里
'django_otp.plugins.otp_totp', # 这里
'two_factor' # 这里
]
...
MIDDLEWARE = [
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django_otp.middleware.OTPMiddleware', # 这里
...
]
LOGIN_URL = 'two_factor:login' # 这里
# 这个是可选的
LOGIN_REDIRECT_URL = 'two_factor:profile' # 这里
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'templates',
],
...
},
]
然后,在core/urls.py
中设置以下路径:
# "core/urls.py"
...
from two_factor.urls import urlpatterns as tf_urls
urlpatterns = [
...
path('', include(tf_urls)) # 这里
]
最后,进行迁移:
python manage.py migrate
这是登录页面:
http://localhost:8000/account/login/
我的问题:
- 如何覆盖
django-two-factor-auth
的Django Admin模板? - 是否有关于
django-two-factor-auth
的Django Admin模板的推荐自定义方式?
英文:
I'm trying to override the templates of django-two-factor-auth for Django Admin but I don't know how to do it. *I don't have frontend with Django. Instead, I have frontend with Next.js
and backend with Django.
This is my django project:
django-project
|-core
| |-settings.py
| └-urls.py
|-my_app1
| |-models.py
| |-admin.py
| └-urls.py
└-templates
And, how I set django-two-factor-auth
following the doc is first, I installed django-two-factor-auth[phonenumbers]
:
pip install django-two-factor-auth[phonenumbers]
Then, set these apps below to INSTALLED_APPS, OTPMiddleware
to MIDDLEWARE, LOGIN_URL and LOGIN_REDIRECT_URL in core/settings.py
as shown below:
# "core/settings.py"
INSTALLED_APPS = [
...
'django_otp', # Here
'django_otp.plugins.otp_static', # Here
'django_otp.plugins.otp_totp', # Here
'two_factor' # Here
]
...
MIDDLEWARE = [
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django_otp.middleware.OTPMiddleware', # Here
...
]
LOGIN_URL = 'two_factor:login' # Here
# this one is optional
LOGIN_REDIRECT_URL = 'two_factor:profile' # Here
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'templates',
],
...
},
]
Then, set the path below to core/urls.py
:
# "core/urls.py"
...
from two_factor.urls import urlpatterns as tf_urls
urlpatterns = [
...
path('', include(tf_urls)) # Here
]
Finally, migrate:
python manage.py migrate
And, this is Login page:
http://localhost:8000/account/login/
My questions:
- How can I override the templates of
django-two-factor-auth
for Django Admin? - Are there any recommended customizations for the templates of
django-two-factor-auth
for Django Admin?
答案1
得分: 1
你可以通过将two_factor/templates/two_factor/
文件夹从你的虚拟环境中复制到templates
文件夹来覆盖Django Admin的django-two-factor-auth模板,如下所示:
django-project
|-core
| |-settings.py
| └-urls.py
|-my_app1
| |-models.py
| |-admin.py
| └-urls.py
└-templates
└-two_factor # 这里
|-core
| |-backup_tokens.html
| |-login.html
| |-otp_required.html
| |-phone_register.html
| |-setup_complete.html
| └-setup.html
|-profile
| |-disable.html
| └-profile.html
|-twilio
| |-press_a_key.xml
| |-sms_message.html
| └-token.xml
|-_base_focus.html
|-_base.html
|-_wizard_actions.html
└-_wizard_forms.html
我建议你修改_base.html如下,以删除提供名为...的模板
消息并添加Home按钮以返回到Django Admin的Home页面:
{% load i18n %} {% <- 添加 %}
<!DOCTYPE html>
<html>
<head>
...
</head>
<body> {% ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ 删除 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ %}
{% comment %} <div class="alert alert-success"><p class="container">提供名为
<code>two_factor/_base.html</code> 的模板以样式化此页面并删除此消息。</p></div> {% endcomment %}
{% ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 添加 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ %}
{% block content_wrapper %}
<div class="container">
{% block content %}{% endblock %}
</div>
{% endblock %}
{% ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ 添加 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ %}
{% if request.user.is_authenticated %}
<hr>
<div class="container">
<div class="row">
<div class="col text-center">
<a class="btn btn-success col-6" href="{% url 'admin:index' %}">{% trans "Home" %}</a>
</div>
</div>
</div>
{% endif %}
{% ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 添加 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ %}
</body>
</html>
我还建议你修改login.html如下,以在身份验证后自动重定向到Django Admin的Home页面:
{% extends "two_factor/_base_focus.html" %}
{% load i18n %}
{% load two_factor_tags %}
{% block extra_media %}
{% ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ 添加 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ %}
<script>
{% if request.user.is_authenticated and request.user.is_staff %}
location = "{% url 'admin:index' %}";
{% endif %}
</script>
{% ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 添加 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ %}
{{ form.media }}
{% endblock %}
...
现在,这是登录页面:
这是账户安全性页面:
英文:
You can override the templates of django-two-factor-auth for Django Admin by copying two_factor folder from two_factor/templates/two_factor/
in your virtual environment to templates
folder as shown below:
django-project
|-core
| |-settings.py
| └-urls.py
|-my_app1
| |-models.py
| |-admin.py
| └-urls.py
└-templates
└-two_factor # Here
|-core
| |-backup_tokens.html
| |-login.html
| |-otp_required.html
| |-phone_register.html
| |-setup_complete.html
| └-setup.html
|-profile
| |-disable.html
| └-profile.html
|-twilio
| |-press_a_key.xml
| |-sms_message.html
| └-token.xml
|-_base_focus.html
|-_base.html
|-_wizard_actions.html
└-_wizard_forms.html
And, I recommend to modify _base.html as shown below to remove Provide a template named ...
message and to add Home button to go to Home page in Django Admin:
{% "templates/two_factor/_base.html" %}
{% load i18n %} {% <- Add %}
<!DOCTYPE html>
<html>
<head>
...
</head>
<body> {% ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ Remove ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ %}
{% comment %} <div class="alert alert-success"><p class="container">Provide a template named
<code>two_factor/_base.html</code> to style this page and remove this message.</p></div> {% endcomment %}
{% ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ Remove ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ %}
{% block content_wrapper %}
<div class="container">
{% block content %}{% endblock %}
</div>
{% endblock %}
{% ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ Add ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ %}
{% if request.user.is_authenticated %}
<hr>
<div class="container">
<div class="row">
<div class="col text-center">
<a class="btn btn-success col-6" href="{% url 'admin:index' %}">{% trans "Home" %}</a>
</div>
</div>
</div>
{% endif %}
{% ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ Add ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ %}
</body>
</html>
And, I recommend to modify login.html as shown below to automatically redirect to Home page in Django Admin if authenticated:
{% "templates/two_factor/core/login.html" %}
{% extends "two_factor/_base_focus.html" %}
{% load i18n %}
{% load two_factor_tags %}
{% block extra_media %}
{% ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ Add ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ %}
<script>
{% if request.user.is_authenticated and request.user.is_staff %}
location = "{% url 'admin:index' %}";
{% endif %}
</script>
{% ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ Add ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ %}
{{ form.media }}
{% endblock %}
...
Now, this is Login page:
And, this is Account Security page:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论