2017-08-11 163 views
0

我有以下功能來更改密碼並顯示用戶配置文件,但是在提交表單而不是被重定向到'profile/'頁面時出現錯誤,說明爲The view core.views.change_password didn't return an HttpResponse object. It returned None instead.,這是爲什麼?更改密碼Django

from django.contrib.auth.forms import UserChangeForm, PasswordChangeForm 
from django.contrib.auth import update_session_auth_hash 

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

     if form.is_valid(): 
      form.save() 
      update_session_auth_hash(request, form.user) #this function keeps the user logged in afte they change their password 
      # request.user could not have been passed in because that would pass in 'AnonymousUser', however 
      #form.user gets the user who was filling in the form and passes it to the function 
      return redirect('/profile') 
    else: 
     form = PasswordChangeForm(user = request.user) 
     args = {'form': form} # gives access to the form in the template 
     return render(request, 'core/change_password.html', args) 

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

注:個人資料頁並在用戶登錄被重定向到他們的個人資料頁,沒有問題後,對網站中的其他部分,例如。

+1

表單無效時會發生什麼?答:沒有。 –

回答

0

您應該將最後兩行移出else塊。這樣,當表單無效時,您將返回POST請求的響應。

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

     if form.is_valid(): 
      ... 
      return redirect('/profile') 
    else: 
     form = PasswordChangeForm(user = request.user) 
    args = {'form': form} # gives access to the form in the template 
    return render(request, 'core/change_password.html', args)