ログインしたユーザーとしてパスワードを変更しようとすると、いつでも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が正しく処理していますか?どんなポインタであれ大いに感謝します!
ご覧になっているトレースバックを質問に追加できますか? – Colwin
確かに@Colwin私は元の質問投稿にそれを加えました – mzayas