在哪里放置自定义模板

在哪里放置自定义模板

本文介绍了Django auth:在哪里放置自定义模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Django(1.9)设置用户身份验证。如文档中所述,我在项目的 urls.py 中包括了auth视图,例如

I want to set up user authentication with Django (1.9). As described in the documentation I included the auth view in my project's urls.py like

urlpatterns = [
    ...,
    url('^accounts/', include('django.contrib.auth.urls')),
    ...,
]

作为描述了一种需要为Auth视图编写自定义模板的方法。我将这些模板放在目录 myproject / templates / registration / 中。现在的问题是,由于这些模板遵循预定义的命名约定,因此它们与Auth视图的管理模板冲突。例如。如果我遵循管理员中的更改密码链接,则管理员视图将使用我的自定义模板呈现。如何为自定义模板命名空间,以免它们干扰管理员?

as the documentation describes, one needs to write custom templates for the Auth views. I put those templates in the directory myproject/templates/registration/. The problem is now that these templates, since they follow the predefined naming convention, clash with the admin templates for Auth views. E.g. if I follow in the link CHANGE PASSWORD in the admin, the admin view gets rendered with my custom template. How can I namespace my custom templates, so that they won't interfere with the admin?

推荐答案

您应覆盖内置函数的模板名称:

You should override template names for built-in functions:

urlpatterns = [
    url(
        '^change-password/',
        auth_views.password_change,
        {'template_name': 'myproject/registration/change-password.html'}
    )
]

As该文档描述了,您必须使用urls.py中的下一个模板:

As the doc describes, you have to use the next templates in urls.py:

^login/$ [name='login']
^logout/$ [name='logout']
^password_change/$ [name='password_change']
^password_change/done/$ [name='password_change_done']
^password_reset/$ [name='password_reset']
^password_reset/done/$ [name='password_reset_done']
^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$ [name='password_reset_confirm']
^reset/done/$ [name='password_reset_complete']

这篇关于Django auth:在哪里放置自定义模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 04:20