2017-06-01 78 views
0

每當我試圖改變我的密碼作爲登錄的用戶,我收到一個AttributeError。我使用Django的內置PasswordChangeForm。誰能幫忙?Django的PasswordChangeForm AttributeError的:「PasswordChangeForm」對象有沒有屬性「cleaned_data」

這裏是我的views.py:

from django.shortcuts import render, redirect 
from accounts.forms import (
    RegistrationForm, 
    EditProfileForm 
) 
from django.contrib.auth.models import User 
from django.contrib.auth.forms import (
    UserChangeForm, 
    PasswordChangeForm 
) 

# Create your views here. 
def home(request): 
    numbers = [1,2,3,4,5] 
    name = 'Matthew Zayas' 
    args = {'myName': name, 'numbers': numbers} 
    return render(request, 'accounts/home.html') 

def register(request): 
    if request.method == 'POST': 
     form = RegistrationForm(request.POST) 
     if form.is_valid(): 
      form.save() 
      return redirect('/account') 
    else: 
     form = RegistrationForm() 
     args = {'form': form} 
     return render(request, 'accounts/reg_form.html', args) 

def view_profile(request): 
    args = {'user': request.user} 
    return render(request, 'accounts/profile.html', args) 

def edit_profile(request): 
    if request.method == 'POST': 
     form = EditProfileForm(request.POST, instance=request.user) 

     if form.is_valid: 
      form.save() 
      return redirect('/account/profile') 
    else: 
     form = EditProfileForm(instance=request.user) 
     args = {'form': form} 
     return render(request, 'accounts/edit_profile.html', args) 

def change_password(request): 
    if request.method == 'POST': 
     form = PasswordChangeForm(data=request.POST, user=request.user) 

     if form.is_valid: 
      form.save() 
      return redirect('/account/profile') 
    else: 
     form = PasswordChangeForm(user=request.user) 
     args = {'form': form} 
     return render(request, 'accounts/change_password.html', args) 

這裏是我收到的回溯:

Environment: 


Request Method: POST 
Request URL: http://127.0.0.1:8000/account/change-password/ 

Django Version: 1.10 
Python Version: 2.7.10 
Installed Applications: 
['django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'accounts'] 
Installed Middleware: 
['django.middleware.security.SecurityMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.common.CommonMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.middleware.clickjacking.XFrameOptionsMiddleware'] 



Traceback: 

File "/xxxxx/Desktop/env1/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner 
    39.    response = get_response(request) 

File "/xxxxx/Desktop/env1/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 
    187.     response = self.process_exception_by_middleware(e, request) 

File "/xxxxx/Desktop/env1/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 
    185.     response = wrapped_callback(request, *callback_args, **callback_kwargs) 

File "/xxxxx/Desktop/env1/diversely/accounts/views.py" in change_password 
    51.   form.save() 

File "/xxxxx/site-packages/django/contrib/auth/forms.py" in save 
    331.   password = self.cleaned_data["new_password1"] 

Exception Type: AttributeError at /account/change-password/ 
Exception Value: 'PasswordChangeForm' object has no attribute 'cleaned_data' 

我不知道我做了什麼錯在這裏,因爲大部分的工作是正在被Django處理嗎?任何指針將非常感激!

+0

能否請你加你看到你的問題回溯? – Colwin

+0

確保@Colwin我添加到它原來的問題後 – mzayas

回答

0

找到你的問題。該表格只有當你調用is_valid()函數獲取cleaned_data屬性

你在change_password功能線不調用函數

if form.is_valid: 

變化,爲(注意括號()

if form.is_valid(): 

這應該解決您的問題。

+0

這解決了我的問題!當我創建自己的表單來創建用戶並編輯他們的個人資料時,我不需要添加這些括號。 是否因爲Django已經定義了函數is_valid()來包含屬性cleaned_data? – mzayas

+0

太棒了!很高興我能幫上忙。你能接受答案並且贊成嗎? – Colwin

+1

我還是個新手,所以投票不會公開展示,但它被記錄! – mzayas

相關問題