如何覆盖`django-two-factor-auth`用于Django管理员的模板?

huangapple go评论104阅读模式
英文:

How to override the templates of `django-two-factor-auth` for Django Admin?

问题

  1. 我试图覆盖Django Admindjango-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_URLLOGIN_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管理员的模板?

我的问题:

  1. 如何覆盖django-two-factor-auth的Django Admin模板?
  2. 是否有关于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/

如何覆盖`django-two-factor-auth`用于Django管理员的模板?

My questions:

  1. How can I override the templates of django-two-factor-auth for Django Admin?
  2. 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 %}

...

现在,这是登录页面:

如何覆盖`django-two-factor-auth`用于Django管理员的模板?

这是账户安全性页面:

如何覆盖`django-two-factor-auth`用于Django管理员的模板?

英文:

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:

{% &quot;templates/two_factor/_base.html&quot; %}

{% load i18n %} {% &lt;- Add %}

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  ...
&lt;/head&gt;
&lt;body&gt; {% ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ Remove ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ %}
  {% comment %} &lt;div class=&quot;alert alert-success&quot;&gt;&lt;p class=&quot;container&quot;&gt;Provide a template named
    &lt;code&gt;two_factor/_base.html&lt;/code&gt; to style this page and remove this message.&lt;/p&gt;&lt;/div&gt; {% endcomment %}
       {% ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ Remove ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ %}
  {% block content_wrapper %}
    &lt;div class=&quot;container&quot;&gt;
      {% block content %}{% endblock %}
    &lt;/div&gt;
  {% endblock %}
  {% ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ Add ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ %}
  {% if request.user.is_authenticated %}
  &lt;hr&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col text-center&quot;&gt;
        &lt;a class=&quot;btn btn-success col-6&quot; href=&quot;{% url &#39;admin:index&#39; %}&quot;&gt;{% trans &quot;Home&quot; %}&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  {% endif %}
  {% ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ Add ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ %}
&lt;/body&gt;
&lt;/html&gt;

And, I recommend to modify login.html as shown below to automatically redirect to Home page in Django Admin if authenticated:

{% &quot;templates/two_factor/core/login.html&quot; %}

{% extends &quot;two_factor/_base_focus.html&quot; %}
{% load i18n %}
{% load two_factor_tags %}

{% block extra_media %}
{% ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ Add ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ %}
&lt;script&gt;
{% if request.user.is_authenticated and request.user.is_staff %}
location = &quot;{% url &#39;admin:index&#39; %}&quot;;
{% endif %}
&lt;/script&gt;
{% ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ Add ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ %}
  {{ form.media }}
{% endblock %}

...

Now, this is Login page:

如何覆盖`django-two-factor-auth`用于Django管理员的模板?

And, this is Account Security page:

如何覆盖`django-two-factor-auth`用于Django管理员的模板?

huangapple
  • 本文由 发表于 2023年8月4日 06:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定