2015-07-13 49 views
1

我将我的一个项目升级到Django 1.8.3,少数挑战之一是我的自定义注册模板不再被Django访问。Django 1.8自定义注册模板文件夹不再工作

基于此:https://stackoverflow.com/a/19226149/3390630我在我的appname/templates/registration文件夹中有我的自定义注册文件。

由于Django的提出到现在的模板被访问的方式,Django的1.8一些重大变化,不再找我定制的注册文件和我得到这个错误:

NoReverseMatch at /resetpassword/ 
Reverse for 'password_reset_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] 

我尝试添加以下装载机到TEMPLATES设置,但没有运气。

'loaders': [ 
      'django.template.loaders.app_directories.Loader', 
      'django.template.loaders.filesystem.Loader', 
     ] 

我还使用自定义urls用于login, logout, password reset

我的网址

... 
url(r'^resetpassword/$', 'django.contrib.auth.views.password_reset', name='password_reset'), 
url(r'^resetpassword/passwordsent/$', 'django.contrib.auth.views.password_reset_done', name='password_reset_done'), 
url(r'^reset/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm', name='password_reset_confirm'), 
url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete', name='password_reset_complete'), 
... 

任何建议如何让Django的看我的自定义文件夹内的一次?

回答

1

在Django中,reverse()方法用于反转名称,即友好名称,以匹配URL模式。在这里,你的正则表达式'^ resetpassword/$'没有一个名字被颠倒过来。将参数name='password_reset_done'添加到您的网址。喜欢的东西,

url(r'^resetpassword/$', 'django.contrib.auth.views.password_reset', {'post_reset_redirect': reverse_lazy('auth_password_reset_done'), 
name='password_reset_done'), #Where 'auth_password_reset_done' is where you want to redirect post form submission on the reset page. 

此外,你需要重新格式化你的URL的结构,因为它缺乏一些必要的参数,这样的事情,

url(r'^password/reset/$', 
    auth_views.password_reset, 
    {'post_reset_redirect': reverse_lazy('auth_password_reset_done'), 
    'template_name': 'registration/password_reset.html'}, 
    name='auth_password_reset'), 

url(r'^password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$', 
    auth_views.password_reset_confirm, 
    {'post_reset_redirect': reverse_lazy('auth_password_reset_complete'), 
    'template_name' : 'registration/reset_confirm.html'}, 
    name='auth_password_reset_confirm'), 

url(r'^password/reset/complete/$', 
    auth_views.password_reset_complete, 
    {'post_reset_redirect': reverse_lazy('auth_password_reset_complete'), 
    'template_name' : 'reset_complete'}, 
    name='auth_password_reset_complete'), 

url(r'^password/reset/done/$', 
    auth_views.password_reset_done, 
    {'template_name': 'registration/reset_done.html'}, 
    name='auth_password_reset_done'), 
+0

谢谢你的建议。我添加了缺少的'name ='password_reset_done'',但仍然得到相同的错误。还有其他建议吗? – WayBehind

+0

顺便提一下,这是Django在'auth' URLs'url(r'^ password_reset/$',views.password_reset,name ='password_reset')内的内容,' – WayBehind

+1

@WayBehind:这是你的问题,http:// stackoverflow.com/questions/20307473/django-built-in-password-reset-views? – Sentient07

0

这是我为了做获取自定义Registration文件夹。

  1. 移动URLssettings.pyapp/urls.pyproject/urls.py
  2. 变化TEMPLATES'APP_DIRS': False,

希望这可能是帮助他人,感谢@ Sentient07指着我在正确的方向!

2

我也在努力解决这个问题。 解决方案建议:更改'APP_DIRS':False,不好,因为它会停止加载虚拟环境中的任何应用程序模板。

解决方案是将注册模板保存在项目根目录下的templates文件夹内;不在任何应用程序内:appname/templates /。

为此,你必须添加:在设置文件模板

'DIRS': [os.path.join(BASE_DIR, 'templates')], 

定义。

第二个问题,你可能会面对这样做后,仍然会忘记密码模板它会加载Django的管理模板,为此:把'注册'之前'django.contrib。管理员“在INSTALLED_APPS设置文件中。

INSTALLED_APPS = (
    'registration', 
    'django.contrib.admin', 
+0

INSTALLED_APPS项目的顺序对我来说是个问题。谢谢。先前的项目优先于后面的内容是违反直觉的 - django文档中的任何地方解释的基本原理是什么? – michela

+0

它必须与命令django读取应用程序的模板目录。对不起;找不到任何文件。 – electropoet