vendredi 17 février 2017

Django : ValueError at form submission, returned none

I'm working on password change form where take old password, new password and confirm new password. If old password does not match with current user's password, it display error message. Also, if new password and password confirm does not match each other, it displays error message. So far I keep getting error messages and I want to know which part went wrong. Why does it return none?

error message

ValueError at /blog/password_change/blue/
   The view blog.views.password_change didn't return an HttpResponse object. It returned None instead.

urls.py

url(r'^password_change/(?P<username>[-\w.]+)/$', views.password_change, name='password_change'),
url(r'^password_change_done/$', views.password_change_done, name='password_change_done'),

forms.py

class PasswordChangeForm(SetPasswordForm):
error_messages = dict(SetPasswordForm.error_messages, **{
    'password_incorrect': ("Your old password was entered incorrectly. Please enter it again."),
})
oldpassword = forms.CharField(
    label=("Old password"),
    strip=False,
    widget=forms.PasswordInput(attrs={'autofocus': True}),
)

field_order = ['oldpassword', 'password1', 'password2']

def clean_oldpassword(self):
    oldpassword = self.cleaned_data["oldpassword"]
    if not self.user.check_password(oldpassword):
        raise forms.ValidationError(
            self.error_messages['password_incorrect'],
            code='password_incorrect',
        )
    return oldpassword

views.py

@login_required
def password_change(request, username):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)
        if form.is_valid():            
           oldpassword = form.cleaned_data.get('oldpassword')
           password1 = form.cleaned_data.get('password1')
           password2 = form.cleaned_data.get('password2')
           if password1 == password2:
               update_session_auth_hash(request, form.username)
               form.save()
               return HttpResponseRedirect('/blog/password_change_done/') 
           else:
               return render(request, 'blog/detail.html', {'error_message': 'password mismatch'})

    else:
        form = PasswordChangeForm(user=request.user)
        return redirect(reverse('blog:profile', args=[form.user.get_username()]))




Aucun commentaire:

Enregistrer un commentaire