Thursday, May 2, 2013

Re: Right way to modify contrib auth package?

I'm sure that this is all obvious to experienced Django developers, but I thought I'd document what I did for my fellow Django newbies. Again, thanks to Anton for his advice.

In my application directory, I subclassed PasswordChangeForm and SetPasswordForm. Then I added my custom validation code, which is just making sure the password is at least eight characters long:

from django import forms
from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm
from django.utils.translation import ugettext_lazy as _

class MyPasswordChangeForm(PasswordChangeForm):

    new_password1 = forms.CharField(label=_("New password"),
                                    min_length=8,
                                    widget=forms.PasswordInput)
    new_password2 = forms.CharField(label=_("New password confirmation"),
                                    min_length=8,
                                    widget=forms.PasswordInput)

class MySetPasswordForm(SetPasswordForm):

    new_password1 = forms.CharField(label=_("New password"),
                                    min_length=8,
                                    widget=forms.PasswordInput)
    new_password2 = forms.CharField(label=_("New password confirmation"),
                                    min_length=8,
                                    widget=forms.PasswordInput)


Then I imported my new classes into registration\auth_urls.py and added my new classes as parameters to password/change and password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/:

                 url(r'^password/change/$',
      auth_views.password_change,
      {'password_change_form': MyPasswordChangeForm},
      'auth_password_change'),
and
                 url(r'^password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
                auth_views.password_reset_confirm,
                {'set_password_form': MySetPasswordForm}, 

And that's it.

Spork

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

No comments:

Post a Comment