2

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.

3
  • 4
    Please, post the whole traceback for your error Commented Dec 27, 2013 at 20:48
  • Like @sk1p said, without more details about the error, it's hard to give you any help. The traceback will allow others to see where the error happens and the path the code takes to get there. Right now all we know is that an exception is being raised somewhere in code that you haven't provided, probably because it's inside a function you're calling. Commented Dec 27, 2013 at 21:21
  • You are suppose to return cleaned_data for def clean(self) Commented Dec 27, 2013 at 21:26

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

2 Comments

thanks,it works. can you tell me why i can not use clean_password(self) when i use this ,it show error
Because clean_password(self) is called before clean_confirm_password(self) (as is it declared first in your form), so clean_password won't yet have the value of clean_confirm_password in its cleaned_data dictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.