Hi, sorry for the problem.

Well, this is used in registration. As stated in the title, this is
used to prevent "duplication".
Let say an existing user in db, whose username is abc, and email is
1...@acb.com
After testing, I can still register an account with the existing email
address, which means that the code does not work properly....

Full source code



/////code begins




import re
from django import forms
from django.contrib.auth.models import User

class RegistrationForm(forms.Form):
    username = forms.CharField(label=u'Username', max_length=30)
    email = forms.EmailField(label=u'Email')
    password1 = forms.CharField(
        label=u'Password',
        widget=forms.PasswordInput()
    )
    password2 = forms.CharField(
        label=u'Password (Again)',
        widget=forms.PasswordInput()
    )

def clean_password2(self):
    if 'password1' in self.cleaned_data:
        password1 = self.cleaned_data['password1']
        password2 = self.cleaned_data['password2']
        if password1 == password2:
            return password2
        raise forms.ValidationError('Passwords do not match.')

def clean_username(self):
    username = self.cleaned_data['username']
    if not re.search(r'^\w+$', username):
        raise forms.ValidationError('Username can only contain
''alphanumeric characters and the underscore.')
    try:
        User.objects.get(username=username)
    except User.DoesNotExist:
        return username
    raise forms.ValidationError('Username is already taken.')

def clean_email(self):
    email = self.cleaned_data['email']
    try:
        User.objects.get(email=email)
    except User.DoesNotExist:
        return email
    raise forms.ValidationError('This email address has been
registered with an existing user.')







/////// code ends

On Aug 21, 10:05 pm, Steve Holden <holden...@gmail.com> wrote:
> On 8/21/2010 7:23 PM, John Yeukhon Wong wrote:> I don't think this code is 
> working properly
>
> That isn't a very helpful description. For better answers, please
> describe what it is doing that it should not (or what it is not doing
> that it should).
>
>
>
> > from django import forms
> > from django.contrib.auth.models import User
>
> > def clean_email(self):
> >     email = self.cleaned_data['email']
> >     try:
> >         User.objects.get(email=email)
> >     except User.DoesNotExist:
> >         return email
> >     raise forms.ValidationError('This email address has been
> > registered with an existing user.')
>
> > How should I rewrite it properly? Thanks!!
>
> Shouldn't clean_email() be a method of the form it's a part of? You
> appear to have written it as a stand-alone function, so it probably
> isn't being called at all.
>
> regards
>  Steve
> --
> DjangoCon US 2010 September 7-9http://djangocon.us/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to