2013-08-21 34 views
0

我試圖爲知道密碼並可以登錄但想要更改的用戶更改密碼錶單。我編寫了一個表單和東西,但它似乎沒有將用戶對象傳遞到表單中,因爲它表示它是一個NoneType對象。Django窗體沒有收到用戶對象

#forms.py 
class ChangePasswordForm(forms.Form): 
    error_messages = { 
     'wrong_current': _("You did not enter the right password!"), 
     'invalid_new': _("You have entered an invalid new password or you did not enter one!"), 
     'no_match': _("The new passwords do not match!") 
    } 
    current_password = forms.CharField(widget=forms.PasswordInput) 
    new_password1 = forms.CharField(widget=forms.PasswordInput, label="New Password: ") 
    new_password2 = forms.CharField(widget=forms.PasswordInput, label="Repeat New Password: ") 

    def __init__(self, *args, **kwargs): 
     self.user = kwargs.pop('user', None) 
     print self.user 
     super(ChangePasswordForm, self).__init__(*args, **kwargs) 

    def clean(self): 
     try: 
      a = u.objects.get(username__iexact=self.user) 
     except u.DoesNotExist: 
      pass 
     if self.cleaned_data['new_password1'] != self.cleaned_data['new_password2']: 
      raise forms.ValidationError(self.error_messages['no_match']) 
     if a.check_password(self.cleaned_data["current_password"]) == "False": 
      raise forms.ValidationError(self.error_messages["wrong_current"]) 
     return self.cleaned_data 
#views.py 
def change_pass_view(request): 
    if request.method =="POST": 
     form = ChangePasswordForm(request.POST) 
     if form.is_valid(): 
      user = u.objects.get(username__iexact=request.user) 
      user.set_password(form.cleaned_data["new_password1"]) 
      user.save() 
     else: 
      print "invalid" 
      a = request.user 
      a = u.objects.get(username__iexact=a) 
      form = UserModificationForm(instance=a) 
      form2 = ChangeEmailForm() 
      form3 = ChangePasswordForm(user=request.user) 
      return render(request, 'accounts/modify.html', {'form': form, 'form2': form2, 'form3': form3, 'usr':request.user, 'email':request.user.email}) 
    else: 
     return redirect('/')  

我需要一些幫助!

+0

您登錄了嗎? – karthikr

+0

是的我已登錄。 –

+1

您忘記了在第一個ChangePasswordForm表格中傳遞用戶 – mariodev

回答

0

我找到了答案。我沒有意識到,在Django中我已經有了一個預先製作好的表單。它現在完美。

相關問題