【问题标题】:Django: Template search order for the authentication system (to create my own auth templates)Django:身份验证系统的模板搜索顺序(创建我自己的身份验证模板)
【发布时间】:2016-04-13 21:23:30
【问题描述】:

我使用的是Django(v1.8.12)的默认认证系统,但是我想创建自己的模板(实际上根据文档django.contrib.auth不提供任何模板)。

然后我在我的应用程序“mainApp”的“模板”文件夹中添加了文件夹“注册”。根据文档,这是正确的位置,名为“registration”的文件夹。

我还创建了模板“login.html”,并且该模板在定义的 url '...account/login' 处正确加载。

问题出在其他模板上:

  • logged_out.html
  • password_change_done.html
  • password_change_form.html
  • password_reset_complete.html
  • password_reset_confirm.html
  • password_reset_done.html
  • password_reset_email.html
  • password_reset_form.html

以前的模板是从“管理”应用程序而不是我的应用程序加载的。

我如何告诉 Django 应该加载我的“注册”模板而不是管理应用程序中定义的模板?

我的项目 url.py 是

urlpatterns = [
    url(r'^', include('mainApp.urls', namespace="mainApp")),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^account/', include('django.contrib.auth.urls')),
]

我的模板设置是默认设置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug': True,
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

【问题讨论】:

  • 好的。我刚刚在模板设置中尝试过这个,现在正在使用'DIRS': ['mainApp/templates'],。无论如何,我愿意接受一个更通用的解决方案,你可以给我:)。
  • 更新以前的解决方案在生产中不起作用(我的意思是在服务器中)

标签: django templates authentication


【解决方案1】:

只需在INSTALLED_APPS 设置中将mainApp 移动到django.contrib.admin 上方,这样Django 就会首先搜索mainApp/templates 目录。

这样,您无需将mainApp/templates 添加到您的DIRS 设置中。

【讨论】:

  • 感谢您的快速回答。将我的应用程序添加到INSTALLED_APPS 的顶部有什么不好的含义吗?我认为文档建议在默认列表的末尾添加我们自己的应用程序。
  • 大多数时候应该没问题,只要你知道你的应用程序的模板、静态文件等会被优先选择。有时可能存在依赖问题,例如,如果您调用admin.site.unregister(User) 并为User 注册您自己的模型管理员。在这种情况下,您的应用需要低于django.contrib.auth
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多