2015-07-13 47 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