如何覆盖Django Admin的`django-two-factor-auth`模板?

bqucvtff  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(124)

我试图覆盖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

字符串
然后,我如何在the doc之后设置django-two-factor-auth,首先,我安装了django-two-factor-auth[phonenumbers]

pip install django-two-factor-auth[phonenumbers]


然后,将下面这些应用程序设置为INSTALLED_APPS,将OTPMiddleware设置为MIDDLEWARE,将core/settings.py中的LOGIN_URL和LOGIN_REDIRECT_URL设置为如下所示:

# "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',
        ],
        ...
    },
]


然后,将下面的路径设置为core/urls.py

# "core/urls.py"

...
from two_factor.urls import urlpatterns as tf_urls

urlpatterns = [
   ...
   path('', include(tf_urls)) # Here
]


最后,迁移:

python manage.py migrate


这里是登录页面:

http://localhost:8000/account/login/


的数据
我的问题:
1.如何覆盖Django Admin的django-two-factor-auth模板?

  1. Django Admin的django-two-factor-auth模板有什么推荐的自定义设置吗?
lndjwyie

lndjwyie1#

您可以通过将two_factor文件夹从虚拟环境中的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 # 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

字符串
并且,我建议修改_base.html,如下图所示,删除Provide a template named ...消息,并在Django管理中添加Home按钮以转到Home页面:

{% "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>


并且,我建议修改login.html,如下所示,在Django Admin中自动重定向到Home页面,如果通过认证:

{% "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 %}

...


现在是登录页面:


的数据
这里是账户安全页面:


相关问题