Skip to main content
 首页 » 编程设计

python之django 身份验证 : password change template customization

2024年08月26日31thcjp

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

This海报被告知在 settings.py 中调整应用顺序,这对我来说有点脆弱。我想this poster可能得到了合理的答案,但如果是这样,我还没有完全理解。

所以我所做的是在我的 urls.py 文件中添加一堆非 DRY 垃圾,从 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 鼓励更清洁的东西。有什么指点吗?

(首先,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。