Anyway, I have found the answer to my own question. Below is the modified views.py code.
def register(request): validators = [MinimumLengthValidator, NumberValidator, UppercaseValidator] if request.method == 'POST': # some code password = request.POST('password') try: for validator in validators: validator().validate(password) except ValidationError as e: messages.error(request, str(e)) return redirect('register')Regards,
Amitesh Sahay91-750 797 8619
On Wednesday, 6 February, 2019, 11:48:21 am IST, 'Amitesh Sahay' via Django users <django-users@googlegroups.com> wrote:
I have an existing django views.py where I have some password, username, and email validation logic applied. However, going forward I need to apply more advanced password validation. for e.g. password length limitation, uppercase sensitivity etc. I have a code written for advanced validation, but I am not able to apply them to my existing views.py. Below is the code from views.py
fromdjango.shortcutsimportrender,redirectfromdjango.contrib.auth.modelsimportUserfromdjango.contribimportmessagesfrom.importvalidatordefregister(request):ifrequest.method =='POST':first_name =request.POST['first_name']last_name =request.POST['last_name']email =request.POST['email']username =request.POST['username']password =request.POST['password',validator.MinimumLengthValidator]password2 =request.POST['password2']# check if the password matchifpassword ==password2:ifUser.objects.filter(username=username).exists():messages.error(request,'username already exist')returnredirect('register')else:ifUser.objects.filter(email=email).exists():messages.error(request,'Registration Failed - Try different email address')returnredirect('register')else:user =User.objects.create_user(username=username,password=password,email=email,first_name=first_name,last_name=last_name)user.save()messages.success(request,'Registration complete, please proceed to login')returnredirect('register')else:messages.error(request,'password dose not match')returnredirect('register')else:returnrender(request,'ACCOUNTS/register.html')
Below is the code for advanced password validation from validate.py
importrefromdjango.core.exceptionsimportValidationErrorfromdjango.utils.translationimportugettextas_classMinimumLengthValidator:def__init__(self,min_length=8):self.min_length =min_lengthdefvalidate(self,password,user=None):iflen(password)<self.min_length:raiseValidationError(_("This password must contain at least %(min_length)d characters."),code='password_too_short',params={'min_length':self.min_length},)defget_help_text(self):return_("Your password must contain at least %(self.min_length)d characters."%{'min_length':self.min_length})classNumberValidator(object):defvalidate(self,password,user=None):ifnotre.findall('\d',password):raiseValidationError(_("The password must contain at least %(min_digits)d digit(s), 0-9."),code='password_no_number',)defget_help_text(self):return_("Your password must contain at least 1 digit, 0-9.")classUppercaseValidator(object):defvalidate(self,password,user=None):ifnotre.findall('[A-Z]',password):raiseValidationError(_("The password must contain at least 1 uppercase letter, A-Z."),code='password_no_upper',)defget_help_text(self):return_("Your password must contain at least 1 uppercase letter, A-Z.")
I have tried below steps.
I imported the validator.pyinthe views.py,andtried to call the module.function inside the password fieldasbelowpassword =request.POST['password',validator.MinimumLengthValidator]
But that doesn't work. If I am right, I can write a mixin class and call it in my views.py. But I am using function based views. So, I am not sure if I can use mixin. Please suggest how can we achieve the desired result.
Regards,
Amitesh Sahay
91-750 797 8619
--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/894430447.3071951.1549433756290%40mail.yahoo.com.
For more options, visit https://groups.google.com/d/optout.
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/894430447.3071951.1549433756290%40mail.yahoo.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment