Django, ModelForms, User and UserProfile - not hashing password

Posted by IvanBernat on Stack Overflow See other posts from Stack Overflow or by IvanBernat
Published on 2010-05-29T19:12:53Z Indexed on 2010/05/29 19:22 UTC
Read the original article Hit count: 412

Filed under:
|

I'm trying to setup a User - UserProfile relationship, display the form and save the data.

When submitted, the data is saved, except the password field doesn't get hashed.

Additionally, how can I remove the help_text from the username and password (inherited from the User model)?

Full code is below, excuse me if it's too long.

Models.py

USER_IS_CHOICES = (
('u', 'Choice A'),
('p', 'Choice B'),
('n', 'Ninja'),
)

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    user_is = models.CharField(max_length=1, choices=USER_IS_CHOICES)

Forms.py

class UserForm(forms.ModelForm):
class Meta:
    model = User
    fields = ["first_name", "last_name", "username",  "email", "password"]

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

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ['user_is']

Views.py

if request.method == 'POST':
    uform = UserForm(request.POST)
    pform = UserProfileForm(request.POST)
    if uform.is_valid() and pform.is_valid():
        user = uform.save()
        profile = pform.save(commit = False)
        profile.user = user
        profile.save()
        return HttpResponseRedirect('/')
else:
    uform = UserForm()
    pform = UserProfileForm()
variables = RequestContext(request, {
    'uform':uform,
    'pform':pform
})
return render_to_response('registration/register.html', variables)

© Stack Overflow or respective owner

Related posts about django

Related posts about django-forms