【问题标题】:Why is my registration/reset_password_done.html template not used by Django?为什么 Django 不使用我的 registration/reset_password_done.html 模板?
【发布时间】:2011-08-09 14:59:58
【问题描述】:

我有一个 urls.py:

from django.contrib.auth.views import (password_reset, password_reset_done, 
                                       password_change, password_change_done)

 [...]
 urlpatterns += i18n_patterns('',   
        url(r'^accounts/password_reset/$', password_reset, 
             {'template_name': 'registration/password_reset.html',
              "post_reset_redirect": '/accounts/password_reset_done/' },
             name='reset-password'),
        url(r'^accounts/password_reset_done/$', password_reset_done, 
             {'template_name': 'registration/password_reset_done.html'},
             name="reset-password-done"))

以及 app/templates/registration 中的相关模板。

password_reset 会显示我的模板,但password_reset_done 不会(它显示同名的管理模板)。在其中运行 pdb 确认传递了正确的模板名称。我三次检查了模板的名称。

【问题讨论】:

  • 是使用默认模板还是说“找不到模板”?
  • 引用我自己的话:“它显示了同名的管理模板”

标签: django templates view django-admin authentication


【解决方案1】:

我终于找到了答案:

应用模板加载器将渲染他找到的第一个与名称匹配的模板,此顺序由您的应用在 INSTALLED_APPS 中的顺序决定。

只需在 INSTALLED_APPS 开头添加包含我的模板的应用程序名称即可解决问题。

【讨论】:

    【解决方案2】:

    如果将您的 password_reset_done.html 的名称更改为 password_reset_done2.html 并相应地更改 urls.py 呢?这个和admin模板还有冲突吗?

    此外,它可能与 urlpatterns += i18n_patterns... 代码出现的位置有关。它发生在默认管理员网址之前还是之后:

    (r'^admin/', include(<location of default admin urls>)),
    

    .. 或任何它的样子。也许他们会覆盖您的自定义网址?

    【讨论】:

    • 我在包含管理员之后发生。但我之前尝试过包含它,但效果并不好。
    • 您的解决方法奏效了。我会接受你的回答,即使它没有回答问题,因为无论如何你是唯一的一个并且确实解决了我的问题。
    猜你喜欢
    • 2016-12-21
    • 1970-01-01
    • 2012-08-14
    • 2022-01-15
    • 1970-01-01
    • 2019-07-02
    • 2016-04-21
    • 2023-04-03
    相关资源
    最近更新 更多