Django PasswordChangeForm AttributeError:'PasswordChangeForm'对象没有属性'cleaning_data'

时间:2017-06-01 02:22:18

标签: python django django-forms

每当我尝试以登录用户身份更改密码时,都会收到attributeError。我正在使用Django的内置PasswordChangeForm。有人可以帮忙吗?

这是我的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处理的?任何指针都会非常感激!

1 个答案:

答案 0 :(得分:0)

找到你的问题。只有在调用cleaned_data函数

时,表单才会获得is_valid()属性

您在change_password函数中的行未调用函数

if form.is_valid:

将其更改为(请注意括号()

if form.is_valid():

这应该可以解决你的问题。