Python Django - form.is_valid() keeps throwing unicode object has no attribute get error -
i creating form custom user type. each time submit form test it, returns error says unicode object has no attribute error.
forms.py:
class registrationform(forms.modelform): """ form registering new account. """ password1 = forms.charfield(widget=forms.passwordinput, label="password") password2 = forms.charfield(widget=forms.passwordinput, label="password (again)") class meta: model = student fields = ('firstname', 'lastname', 'email', 'password1', 'password2', 'is_second_year') def clean(self): # check 2 password entries match password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 , password2 , password1 != password2: raise forms.validationerror("passwords don't match") return password2 def save(self, commit=true): user = super(registrationform, self).save(commit=false) user.set_password(self.cleaned_data['password1']) if commit: user.save() return user
models.py:
class studentmanager(baseusermanager): def create_user(self, firstname, lastname, email, is_second_year, password, is_officer=false, hours=0, points=0): if not email: raise valueerror("student must have email address.") newstudent = self.model( email = self.normalize_email(email), firstname=firstname, lastname=lastname, is_officer=is_officer, is_second_year=is_second_year, hours=hours, points=points ) newstudent.set_password(password) user.save(using=self.db) return newstudent def create_superuser(self, firstname, lastname, email, password, is_second_year, is_officer=true, hours=0, points=0): newsuperstudent = self.create_user( email = self.normalize_email(email), firstname=firstname, lastname=lastname, is_officer=is_officer, is_second_year=is_second_year, hours=hours, points=points, password=password ) newsuperstudent.is_admin = true newsuperstudent.save(using=self.db) return newsuperstudent class student(abstractbaseuser): firstname = models.charfield(verbose_name="first name", max_length=30, default="") lastname = models.charfield(verbose_name="last name", max_length=30, default="") email = models.emailfield( verbose_name='email address', max_length=255, unique=true, default='' ) is_officer = models.booleanfield(default=false) is_second_year = models.booleanfield(verbose_name="check box if second year in nhs") hours = models.decimalfield(max_digits=5, decimal_places=2) points = models.decimalfield(max_digits=3, decimal_places=1) username_field = 'email' def __unicode__(self): return self.username
views.py:
def sign_up(request): # if post request need process form data if request.method == 'post': # create form instance , populate data request: form = registrationform(request.post) # check whether it's valid: if form.is_valid(): print "money" form.save() # redirect new url: return httpresponseredirect('/sign_up_success/') # if (or other method) we'll create blank form args = {} args.update(csrf(request)) args['form'] = registrationform() return render_to_response('events/sign_up.html', args)
error location within .is_valid() method:
field_value = self.cleaned_data.get(field, none)
my guess reason, cleaned_data attribute of form unicode object rather dictionary. have no clue why so!
clean
must return full cleaned_data
dictionary, not single string field.
Comments
Post a Comment