Search code examples
pythondjangodjango-formwizard

Django FormWizard. Need to set_password


I am trying to use FormWizard to make a registration for new users. I currently have a problem with passing password after all validations is done. I tried to do it numerous ways, but always get an error. The traceback is listed below:

Traceback:

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\formtools\wizard\views.py" in dispatch
  237.         response = super(WizardView, self).dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in dispatch
  89.         return handler(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\formtools\wizard\views.py" in post
  300.                 return self.render_done(form, **kwargs)
File "C:\Python27\lib\site-packages\formtools\wizard\views.py" in render_done
  357.                                   **kwargs)
File "C:\Users\U-60.DOM\Source\Repos\Casting\Casting\Casting\Casting\app\views.py" in done
  940.         passd=past_data.get('password1')

Exception Type: AttributeError at /registration_steps
Exception Value: 'NoneType' object has no attribute 'get'

forms.py

class ApplicantForm1(forms.ModelForm):
    password1 = forms.CharField(
        label='Password',
        widget=forms.PasswordInput
    )
    password2 = forms.CharField(
        label='confirm',
        widget=forms.PasswordInput
    )
    def clean_password2(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError('Passwords dont match')
        return password2
    class Meta:
        model = ExtUser
        fields = ['email','firstname', 'lastname', 'middlename', 'date_of_birth', 'gender', 'family_position',
                  'driver_license', 'driver_license_category',
                  ]

views.py

class ApplicantWizard(SessionWizardView):
def get_template_names(self):
    return [TEMPLATES_REG[self.steps.current]]
def get_form_instance( self, step ):
   # if self.instance is None:
   #     self.instance = ExtUser()
    return self.instance
def done(self, form_list, **kwargs):
    past_data =  self.get_cleaned_data_for_step('1')
    passd=past_data.get('password1')
    user = self.instance.save()
    user.set_password(passd)

        return render_to_response('app/successpage.html', {
            'title':"Registration complited" ,
        })

Solution

  • Without the full traceback I'll assume it's because you're trying to set the password before you've created the user.

    Try to do;

        def done(self, form_list, **kwargs):
            past_data =  self.get_cleaned_data_for_step('1')
            passd=past_data(['password1'])
            user = self.instance.save()
            user.set_password(passd)
    
            return render_to_response('app/successpage.html', {
                'title':"Registration completed" ,
            })
    

    edit

    The issue is before that, but the above should still be valid.

    You're calling past_data when it's a dict I believe that is returned by get_cleaned_data_for_step so you need to do passd=past_data.get('password1')