Tuesday, February 5, 2019

Re: reg: How to use Django Password validation in my existing views?

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 Sahay
91-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

from django.shortcuts import render, redirect  from django.contrib.auth.models import User  from django.contrib import messages  from . import validator  def register(request):      if request.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 match          if password == password2:                if User.objects.filter(username=username).exists():                  messages.error(request, 'username already exist')                  return redirect('register')              else:                  if User.objects.filter(email=email).exists():                      messages.error(request, 'Registration Failed - Try different email address')                      return redirect('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')                      return redirect('register')          else:              messages.error(request, 'password dose not match')              return redirect('register')      else:          return render(request, 'ACCOUNTS/register.html')

Below is the code for advanced password validation from validate.py

import re      from django.core.exceptions import ValidationError  from django.utils.translation import ugettext as _        class MinimumLengthValidator:      def __init__(self, min_length=8):          self.min_length = min_length        def validate(self, password, user=None):          if len(password) < self.min_length:              raise ValidationError(                  _("This password must contain at least %(min_length)d characters."),                  code='password_too_short',                  params={'min_length': self.min_length},              )        def get_help_text(self):          return _(              "Your password must contain at least %(self.min_length)d characters."              % {'min_length': self.min_length}          )      class NumberValidator(object):      def validate(self, password, user=None):          if not re.findall('\d', password):              raise ValidationError(                  _("The password must contain at least %(min_digits)d digit(s), 0-9."),                  code='password_no_number',              )        def get_help_text(self):          return _(              "Your password must contain at least 1 digit, 0-9."          )      class UppercaseValidator(object):      def validate(self, password, user=None):          if not re.findall('[A-Z]', password):              raise ValidationError(                  _("The password must contain at least 1 uppercase letter, A-Z."),                  code='password_no_upper',              )        def get_help_text(self):          return _(              "Your password must contain at least 1 uppercase letter, A-Z."          )

I have tried below steps.

I imported the validator.py in the views.py,and tried to call the module.function inside the password field as below    password = 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.

No comments:

Post a Comment