0

我对 Django 很陌生,我对一种行为感到困惑,我正在摸索以了解为什么它没有按我期望的方式工作。

我正在为密码更改 ( password_change_form.html) 自定义我的模板,并将它放在我的应用程序模板目录中registration

我正在使用django-registration。在我settings.py的 INSTALLED_APPS 中,我有默认顺序的默认应用程序('django.contrib.auth'等...)、我的应用程序和registration. 管理员已启用。我知道 Django 按照 INSTALLED_APPS 变量中设置的顺序查找模板。

在我的项目 urls.py 我有

urlpatterns = patterns('',
   url(r'', include('website.urls', namespace="website")),
   url(r'^admin/', include(admin.site.urls)),
   (r'^accounts/', include('registration.backends.default.urls')),
)
urlpatterns += staticfiles_urlpatterns()

在我的应用程序 urls.py 我有

urlpatterns = patterns('',
   url(r'^$', views.home, name='home'),
   url(r'^accounts/profile/password/change/$', 
       'django.contrib.auth.views.password_change', 
       {'template_name': 'registration/password_change_form.html'}, 
       name='edit_password'),
)

我的困惑是,如果我将template_name设置留给'registration/password_change_form.html'Django,它将显示默认的密码更改管理表单,而不是我的自定义模板。

我在我的应用程序模板下创建了一个名为的目录,auth并在此位置复制了我的模板。现在,如果我更改template_name'auth/password_change_form.html'Django 将显示我的模板!

我很困惑,因为 Django 没有按照我的指示在 location 使用我的模板,registration但如果它在 location 就会这样做auth

有人可以向我解释 Django 1.5 的底层发生了什么,为什么它只会在它喜欢的目录中使用我的模板?!

4

1 回答 1

0

Yeah, the problem is that your first instruction is the default instruction - the same one that pulls the password_change_form.html in installed app order. IF you were to examine the default value for the template_name field, it would be exactly what you are setting it to. Just because you set the value yourself instead of letting it take the default value - it still processes it the same way. It looks in the order of installed_apps for a template directory with a registration subdirectory, and a password_change_form.html file. So regardless, unless you give it a name that DOES NOT EXIST in the default django auth templates - it won't go where you want (unless you place your app before the auth app in settings). Now, if you want to keep your folder structure the same, just change the name of the file and pass in the correct name to the template_name field.

于 2013-07-18T20:09:09.677 回答