Reputation: 351
I just create a forms for user register The code is here
forms.py
from django import forms
from django.conf import settings
from django.contrib.auth import get_user_model
def lowercase_email(email):
"""
Normalize the address by lowercasing the domain part of the email
address.
"""
email = email or ''
try:
email_name, domain_part = email.strip().rsplit('@', 1)
except ValueError:
pass
else:
email = '@'.join([email_name.lower(), domain_part.lower()])
return email
class SignupForm (forms.ModelForm):
username =forms.CharField(
label='username',required=True,max_length=20,min_length=3)
email = forms.EmailField(
label='email',required=True)
password =forms.CharField(
label='pssword',required=True,max_length=20,min_length=6,widget = forms.PasswordInput)
confirm_password= forms.CharField(
label='confirm_email',required=True,max_length=20,min_length=6,widget = forms.PasswordInput)
class Meta:
model = get_user_model()
fields = ("username","email","password","confirm_password",)
def clean(self):
password = self.cleaned_data["password"]
confirm_password = self.cleaned_data["confirm_password"]
if password and password != confirm_password:
raise forms.ValidationError("password not same")
return password
and the views.py
from django.shortcuts import render_to_response, redirect,render
from django.conf import settings
from django.http import HttpResponse,HttpResponseRedirect,Http404
from django.template import RequestContext
from sns.accounts.forms import SignupForm
from django.contrib.auth import get_user_model
def signup(request):
if request.method=='POST':
form=SignupForm(data=request.POST)
if form.is_valid():
UserModel=get_user_model()
username = form.cleaned_data['username']
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user=UserModel.objects.create_user(username=username,email=email,password=password)
return redirect('/')
else:
form=SignupForm(auto_id=True)
return render_to_response('signup.html',{'form':form},context_instance=RequestContext(request))
Once I test it , it shows error
'str' object has no attribute 'get'
Then I remove the method clean from the forms.py , then everything works well
I do not know where is the problem .As I have just written the same code from others for the clean method.
Upvotes: 2
Views: 9776
Reputation: 3467
change clean(self)
to clean_confirm_password(self)
or
return cleaned_data
for def clean(self)
since clean def expects a dictionary and you are providing a string in form of password variable.
Upvotes: 4
Reputation: 481
It looks like you're missing the call to super's clean() at the start of your clean().
cleaned_data = super(SignupForm, self).clean()
The error message seems to indicate that cleaned_data is a string and not a dictionary; it might help to print cleaned_data to see what it is.
Upvotes: 6