如何使用 django-two-factor-auth 使用多个 _base.html 文件?

How to use more than one _base.html file using django-two-factor-auth?

Django-two-factor-auth 库需要一个 _base.html 文件来自定义集成样式。

我需要我的登录页面的基础与其他两个因素页面的基础不同。我如何使用两个不同的 _base.html 文件而不是对所有两个因素 url 使用相同的 _base.html?

在您的项目目录中,添加一个名为 templates 的文件夹,与您的应用程序处于同一级别,例如 usersblogs 等。在其中,为 two_factor。在此文件夹中,将修改后的 base.html.

如果您以某种方式收到错误消息,请在 TEMPLATES = [ 下的 settings.py 中。替换

'DIRS': [],

'DIRS': [os.path.join(BASE_DIR, 'templates')], # in django2

'DIRS': [BASE_DIR.joinpath('templates')],# in django3

这里是Doc about templates

op自己回答后更新

如果你想 two_factor/templates/two_factor/core/login.html 使用你自己的 base.html,你应该按照我上面提到的,你将有 templates/two_factor/core/login.html。请注意 templates 与您的其他 apps 处于同一目录级别。在templates文件夹中,生成two_factor文件夹,然后在里面生成core文件夹,然后在里面生成login.html。您现在可以随意修改它。

您想覆盖但不更改包目录中内容的原因是,当您更新包时,您必须再次手动进行更改。

上述方法之所以有效,是因为我们在设置中放置了'DIRS': [os.path.join(BASE_DIR, 'templates')],Django 将首先在该文件夹中查找模板。这个方法的详细应用在这里Overriding from the project’s templates directory

我能够通过手动更改包目录 two_factor/templates/two_factor/core/login.html 中引用 _base.html 文件的以下行到我的登录页面的基本模板来解决这个问题。

{% extends "two_factor/_base_focus.html" %}

然后我继续使用 _base.html 自定义其他两个因素页面。