title: Django: How to apply the built-in password validators?
body: <p>I like to test the django built-in validators on my registration form. So i added this...</p>
  
  <hr>
  
  <p><strong>settings.py</strong></p>
  
  <pre><code>AUTH_PASSWORD_VALIDATORS = [
      {
          'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
          'OPTIONS': {
              'min_length': 9,
          }
      },
      {
          'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
      },
  ]
  </code></pre>
  
  <hr>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>from django.contrib.auth.models import User
  from django import forms
  import django.contrib.auth.password_validation as validators
  
  
  class UserRegistrationForm(forms.ModelForm):
      password = forms.CharField(label='Password',
                                 widget=forms.PasswordInput)
      password2 = forms.CharField(label='Repeat password',
                                  widget=forms.PasswordInput)
  
      class Meta:
          model = User
          fields = ('username', 'first_name', 'email')
  
  
      # --- check duplicate
      def clean_password2(self):
          cd = self.cleaned_data
          if cd['password'] != cd['password2']:
              raise forms.ValidationError('Passwords don\'t match.')
          return cd['password2']
  
      # --- django built-in validator
      def pass_validate(self):
          password = self.cleaned_data('password')
          try:
              validators(password, self.instance)
          except forms.ValidationError as error:
              self.add_error('password', error)
          return password
  </code></pre>
  
  <hr>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>&lt;...&gt;
  def register(request):
      if request.method == 'POST':
          user_form = UserRegistrationForm(request.POST)
          if user_form.is_valid():
              # Create a new user object but avoid saving it yet
              new_user = user_form.save(commit=False)
              # Set the chosen password
              new_user.set_password(
                  user_form.cleaned_data['password'])
              # Save the User object
              new_user.save()
              return render(request,
                            'account/register_done.html',
                            {'new_user': new_user})
      else:
          user_form = UserRegistrationForm()
      return render(request,
                    'account/register.html',
                    {'user_form': user_form})
  &lt;...&gt;
  </code></pre>
  
  <hr>
  
  <ul>
  <li>Unfortunately i can still register as a user with passwords like "123". So <em>the min_length</em> does not work. </li>
  <li>The <code>pass_validate</code> Function in forms.py was an approach, to add the django built-in validators, after reading some articles here in the forum. </li>
  <li>The good thing is the server works and shows no errors.</li>
  </ul>
  
  <p>So my question is <em>" How to apply the built-in password validators"</em> in the right way? Do i need i a special validators.py ? But my guess i got the function in forms.py wrong.</p>