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

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

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

问题

  1. 我试图覆盖Django Admindjango-two-factor-auth模板,但我不知道如何做。 *我没有使用Django的前端,而是使用Next.js作为前端,Django作为后端。

这是我的Django项目:

  1. django-project
  2. |-core
  3. | |-settings.py
  4. | └-urls.py
  5. |-my_app1
  6. | |-models.py
  7. | |-admin.py
  8. | └-urls.py
  9. └-templates

而且,我按照文档的说明设置了django-two-factor-auth,首先,我安装了django-two-factor-auth[phonenumbers]

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

然后,如下所示,在core/settings.py中将以下应用程序添加到INSTALLED_APPS中,将OTPMiddleware添加到MIDDLEWARE中,设置LOGIN_URLLOGIN_REDIRECT_URL

  1. # "core/settings.py"
  2. INSTALLED_APPS = [
  3. ...
  4. 'django_otp', # 这里
  5. 'django_otp.plugins.otp_static', # 这里
  6. 'django_otp.plugins.otp_totp', # 这里
  7. 'two_factor' # 这里
  8. ]
  9. ...
  10. MIDDLEWARE = [
  11. ...
  12. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  13. 'django_otp.middleware.OTPMiddleware', # 这里
  14. ...
  15. ]
  16. LOGIN_URL = 'two_factor:login' # 这里
  17. # 这个是可选的
  18. LOGIN_REDIRECT_URL = 'two_factor:profile' # 这里
  19. ...
  20. TEMPLATES = [
  21. {
  22. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  23. 'DIRS': [
  24. BASE_DIR / 'templates',
  25. ],
  26. ...
  27. },
  28. ]

然后,在core/urls.py中设置以下路径:

  1. # "core/urls.py"
  2. ...
  3. from two_factor.urls import urlpatterns as tf_urls
  4. urlpatterns = [
  5. ...
  6. path('', include(tf_urls)) # 这里
  7. ]

最后,进行迁移:

  1. python manage.py migrate

这是登录页面:

  1. 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:

  1. django-project
  2. |-core
  3. | |-settings.py
  4. | └-urls.py
  5. |-my_app1
  6. | |-models.py
  7. | |-admin.py
  8. | └-urls.py
  9. └-templates

And, how I set django-two-factor-auth following the doc is first, I installed django-two-factor-auth[phonenumbers]:

  1. 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:

  1. # "core/settings.py"
  2. INSTALLED_APPS = [
  3. ...
  4. 'django_otp', # Here
  5. 'django_otp.plugins.otp_static', # Here
  6. 'django_otp.plugins.otp_totp', # Here
  7. 'two_factor' # Here
  8. ]
  9. ...
  10. MIDDLEWARE = [
  11. ...
  12. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  13. 'django_otp.middleware.OTPMiddleware', # Here
  14. ...
  15. ]
  16. LOGIN_URL = 'two_factor:login' # Here
  17. # this one is optional
  18. LOGIN_REDIRECT_URL = 'two_factor:profile' # Here
  19. ...
  20. TEMPLATES = [
  21. {
  22. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  23. 'DIRS': [
  24. BASE_DIR / 'templates',
  25. ],
  26. ...
  27. },
  28. ]

Then, set the path below to core/urls.py:

  1. # "core/urls.py"
  2. ...
  3. from two_factor.urls import urlpatterns as tf_urls
  4. urlpatterns = [
  5. ...
  6. path('', include(tf_urls)) # Here
  7. ]

Finally, migrate:

  1. python manage.py migrate

And, this is Login page:

  1. 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模板,如下所示:

  1. django-project
  2. |-core
  3. | |-settings.py
  4. | └-urls.py
  5. |-my_app1
  6. | |-models.py
  7. | |-admin.py
  8. | └-urls.py
  9. └-templates
  10. └-two_factor # 这里
  11. |-core
  12. | |-backup_tokens.html
  13. | |-login.html
  14. | |-otp_required.html
  15. | |-phone_register.html
  16. | |-setup_complete.html
  17. | └-setup.html
  18. |-profile
  19. | |-disable.html
  20. | └-profile.html
  21. |-twilio
  22. | |-press_a_key.xml
  23. | |-sms_message.html
  24. | └-token.xml
  25. |-_base_focus.html
  26. |-_base.html
  27. |-_wizard_actions.html
  28. └-_wizard_forms.html

我建议你修改_base.html如下,以删除提供名为...的模板消息并添加Home按钮以返回到Django Admin的Home页面:

  1. {% load i18n %} {% <- 添加 %}
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. ...
  6. </head>
  7. <body> {% 删除 %}
  8. {% comment %} <div class="alert alert-success"><p class="container">提供名为
  9. <code>two_factor/_base.html</code> 的模板以样式化此页面并删除此消息。</p></div> {% endcomment %}
  10. {% 添加 %}
  11. {% block content_wrapper %}
  12. <div class="container">
  13. {% block content %}{% endblock %}
  14. </div>
  15. {% endblock %}
  16. {% 添加 %}
  17. {% if request.user.is_authenticated %}
  18. <hr>
  19. <div class="container">
  20. <div class="row">
  21. <div class="col text-center">
  22. <a class="btn btn-success col-6" href="{% url 'admin:index' %}">{% trans "Home" %}</a>
  23. </div>
  24. </div>
  25. </div>
  26. {% endif %}
  27. {% 添加 %}
  28. </body>
  29. </html>

我还建议你修改login.html如下,以在身份验证后自动重定向到Django Admin的Home页面:

  1. {% extends "two_factor/_base_focus.html" %}
  2. {% load i18n %}
  3. {% load two_factor_tags %}
  4. {% block extra_media %}
  5. {% 添加 %}
  6. <script>
  7. {% if request.user.is_authenticated and request.user.is_staff %}
  8. location = "{% url 'admin:index' %}";
  9. {% endif %}
  10. </script>
  11. {% 添加 %}
  12. {{ form.media }}
  13. {% endblock %}
  14. ...

现在,这是登录页面:

如何覆盖`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:

  1. django-project
  2. |-core
  3. | |-settings.py
  4. | └-urls.py
  5. |-my_app1
  6. | |-models.py
  7. | |-admin.py
  8. | └-urls.py
  9. └-templates
  10. └-two_factor # Here
  11. |-core
  12. | |-backup_tokens.html
  13. | |-login.html
  14. | |-otp_required.html
  15. | |-phone_register.html
  16. | |-setup_complete.html
  17. | └-setup.html
  18. |-profile
  19. | |-disable.html
  20. | └-profile.html
  21. |-twilio
  22. | |-press_a_key.xml
  23. | |-sms_message.html
  24. | └-token.xml
  25. |-_base_focus.html
  26. |-_base.html
  27. |-_wizard_actions.html
  28. └-_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:

  1. {% &quot;templates/two_factor/_base.html&quot; %}
  2. {% load i18n %} {% &lt;- Add %}
  3. &lt;!DOCTYPE html&gt;
  4. &lt;html&gt;
  5. &lt;head&gt;
  6. ...
  7. &lt;/head&gt;
  8. &lt;body&gt; {% Remove %}
  9. {% comment %} &lt;div class=&quot;alert alert-success&quot;&gt;&lt;p class=&quot;container&quot;&gt;Provide a template named
  10. &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 %}
  11. {% Remove %}
  12. {% block content_wrapper %}
  13. &lt;div class=&quot;container&quot;&gt;
  14. {% block content %}{% endblock %}
  15. &lt;/div&gt;
  16. {% endblock %}
  17. {% Add %}
  18. {% if request.user.is_authenticated %}
  19. &lt;hr&gt;
  20. &lt;div class=&quot;container&quot;&gt;
  21. &lt;div class=&quot;row&quot;&gt;
  22. &lt;div class=&quot;col text-center&quot;&gt;
  23. &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;
  24. &lt;/div&gt;
  25. &lt;/div&gt;
  26. &lt;/div&gt;
  27. {% endif %}
  28. {% Add %}
  29. &lt;/body&gt;
  30. &lt;/html&gt;

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

  1. {% &quot;templates/two_factor/core/login.html&quot; %}
  2. {% extends &quot;two_factor/_base_focus.html&quot; %}
  3. {% load i18n %}
  4. {% load two_factor_tags %}
  5. {% block extra_media %}
  6. {% Add %}
  7. &lt;script&gt;
  8. {% if request.user.is_authenticated and request.user.is_staff %}
  9. location = &quot;{% url &#39;admin:index&#39; %}&quot;;
  10. {% endif %}
  11. &lt;/script&gt;
  12. {% Add %}
  13. {{ form.media }}
  14. {% endblock %}
  15. ...

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
  • django
  • django-admin
  • django-templates
  • django-two-factor-auth
  • python
匿名

发表评论

匿名网友

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

确定