User Registration -> Password not saving

27 views
Skip to first unread message

Kurtis

unread,
Sep 21, 2011, 6:42:02 PM9/21/11
to Django users
Hey,

I've created my own User Registration FormView. Everything seems to
work great except the password is always saved as "!". I can change
that with the "password changer" in the admin section of the site. I
am using an alternative authentication backend, so I'm not sure if
that was causing problems. I went ahead and included the default
authentication backend as well (at the front of the list) but that
didn't seem to change things. I've included my code at the bottom. Any
help would be greatly appreciated! Also, if there's anything wierd
about my code, let me know. I'm still learning.

class SignUpFormView(FormView):

template_name = u'base.html'
form_class = SignUpForm
success_url = '/pages/getting_started'

def form_valid(self, form):

# Gather Data
email = form.cleaned_data['email']
firstName = form.cleaned_data['firstName']
lastName = form.cleaned_data['lastName']
password = form.cleaned_data['password']

# Create a Unique UserName from a Hash
userName_hash = hashlib.md5(email).hexdigest()
userName_hash = userName_hash[:30]

# Create the User
user = User.objects.create_user(userName_hash, email,
password)
user.set_password(password) # Duplicated in hopes of it
working.
user.first_name = firstName
user.last_name = lastName
user.is_active = True

# Save the User
user.save()

return super(SignUpFormView, self).form_valid(form)

def form_invalid(self, form):
return super(SignUpFormView, self).form_invalid(form)

class SignUpForm(forms.Form):

email = forms.EmailField(required = True, label = u'Email
Address')
firstName = forms.CharField(required = True)
lastName = forms.CharField(required = True)
password = PasswordField()
password_confirm = forms.CharField(label = u'Password
Confirmation',
widget = forms.PasswordInput(render_value =
False),
required = True)

# Custom Form Validation
def clean(self):

cleaned_data = self.cleaned_data
password = cleaned_data.get('password')
password_confirm = cleaned_data.get('password_confirm')

# Check Password Matches Confirmation
if password and password_confirm:
if password is not password_confirm:
raise forms.ValidationError(u'Your passwords do not
match.')

return self.cleaned_data

Kurtis

unread,
Sep 21, 2011, 6:44:42 PM9/21/11
to Django users
I didn't realize the code would come out so unreadable. It's also
posted here: http://dpaste.com/618619/

Daniel Roseman

unread,
Sep 22, 2011, 4:15:13 AM9/22/11
to django...@googlegroups.com


On Wednesday, 21 September 2011 23:42:02 UTC+1, Kurtis wrote:
Hey,

I've created my own User Registration FormView. Everything seems to
work great except the password is always saved as "!". I can change
that with the "password changer" in the admin section of the site. I
am using an alternative authentication backend, so I'm not sure if
that was causing problems. I went ahead and included the default
authentication backend as well (at the front of the list) but that
didn't seem to change things. I've included my code at the bottom. Any
help would be greatly appreciated! Also, if there's anything wierd
about my code, let me know. I'm still learning.

<snip>
 
class SignUpForm(forms.Form):

    email = forms.EmailField(required = True, label = u'Email
Address')
    firstName = forms.CharField(required = True)
    lastName = forms.CharField(required = True)
    password = PasswordField()
    password_confirm = forms.CharField(label = u'Password
Confirmation',
                        widget = forms.PasswordInput(render_value =
False),
                        required = True)

    <snip>

What is PasswordField?
-- 
DR. 

Kurtis Mullins

unread,
Sep 22, 2011, 10:18:15 AM9/22/11
to django...@googlegroups.com
Sorry, I guess I should've posted that as well :)

import string
class PasswordField(forms.CharField):
   
    # Setup the Field
    def __init__(self, *args, **kwargs):
        super(PasswordField, self).__init__(min_length = 7, required = True,
                        label = u'Password',

                        widget = forms.PasswordInput(render_value = False),      
                        *args, **kwargs)
   
    # Validate - 1+ Numbers, 1+ Letters
    def clean (self, value):
       
        # Setup Our List of Characters
        lower = list(string.lowercase[:26])
        upper = list(string.uppercase[:26])
        numbers = [str(i) for i in range(10)]
       
        # Assume False until Proven Otherwise
        numCheck = False
        charCheck = False

        # Loop until we Match       
        for char in value:
            if not charCheck:
                if char in lower or char in upper:
                    charCheck = True
            if not numCheck:
                if char in numbers:
                    numCheck = True
            if numCheck and charCheck:
                break
       
        if not numCheck or not charCheck:
            raise forms.ValidationError(u'Your password must include at least \
                                          one letter and at least one number.')

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/UpH1hn6WWJwJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Daniel Roseman

unread,
Sep 22, 2011, 11:56:30 AM9/22/11
to django...@googlegroups.com
You need to ensure that it returns the value if it is valid. Currently, your code raises an error if it is invalid, but returns `None` if it is valid - you should explicitly return `value`.
Actually, even better, you should call the superclass `clean` method and return the value from that:
    return super(PasswordField, self).clean(value)
--
DR.
 

Kurtis Mullins

unread,
Sep 22, 2011, 12:04:39 PM9/22/11
to django...@googlegroups.com
Thanks Daniel! You were right on cue with that. I tried your code and for some reason the value is being returned as a standard string instead of a unicode string, as far as I can tell. (Effectively, my password and passwordConfirm are not matching anymore). Hopefully once I get that worked out, things will be working great.

DR.
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/96liwPhxphwJ.

Kurtis Mullins

unread,
Sep 22, 2011, 12:09:34 PM9/22/11
to django...@googlegroups.com
I was wrong. There wasn't any issue with the unicode. Your fixed worked perfectly. I just had to change my comparison from

password is not password_confirm

to

password != password_confirm

and it's working great now. Thanks a lot!
Reply all
Reply to author
Forward
0 new messages