python - Django 身份验证 : password change template customization

标签 python django templates

我想将 django auth 的密码更改模板主题设置到我的网站。问题是 django 看到的是 django/contrib/admin/templates/registration/ 版本的模板,而不是我精心制作的 myapp/template/registration/password*.html >.

This海报被告知要在settings.py中摆弄应用程序顺序,这对我来说有点脆弱。我认为this发帖人可能已经得到了合理的答案,但如果是这样,我还不太明白。

所以我所做的就是向我的 urls.py 文件添加一堆非 DRY cruft,该文件是从 auth_urls.py 复制的:

# Provide explicit templates where I've provided them, shadowing the URL's
# that registration.backends.defaults.urls will provide.
url(r'^accounts/password/change/$',
    auth_views.password_change,
    {'post_change_redirect': reverse_lazy('auth_password_change_done'),
     'template_name': 'registration/password/change_form.html' },
    name='auth_password_change'),
# ...
url(r'^accounts/', include('registration.backends.default.urls')),

这确实有效,但它的重复让我感到难过。当然,django 鼓励更清洁的东西。有什么指点吗?

(Fwiw,django 1.7 和 python 3.4.2。)

最佳答案

将此模板放入项目的 templates 目录中,并将以下代码添加到 settings.py 中。

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

默认情况下,django.template.loaders.filesystem.Loader 放置在 TEMPLATE_LOADERS 中的 django.template.loaders.app_directories.Loader 之前,因此项目的 templates 目录优先于应用的 templates

关于python - Django 身份验证 : password change template customization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28010276/

相关文章:

Python:根据多个值+总和合并n个字典

python - 如何将 Octave 添加到我的 PATH 以使用 oct2py?

python - 将 ManyToMany 转换为 ForeignKey (django)

c++ - 为什么模板只能在头文件中实现?

python - 如何创建独立的非模态对话框

python - Matplotlib 未知属性 "headwidth"和 "head_width"

python - Django:如何在测试 View 时模拟文件上传

python - SQLAlchemy 和 django,准备好生产了吗?

c++ - 如果不直接使用,模板参数会丢失左值引用

c++ - 显式特化函数模板的正确方法