title: When I try to validate 'username' field in Django form with clean_method I get a NameError
body: <p>Here is my forms.py file. I got a &quot;NameError: name 'username' is not defined&quot; on this row:</p>
  <p><code>def clean_username(self):</code></p>
  <p>trying to submit this form, but, AFAIK, there are nothing wrong with it.</p>
  <pre><code>from .models import Blog
  from django.contrib.auth.models import User
  from django.contrib.auth.forms import UserCreationForm
  from django.core.exception import ValidationError
  
  class PostForm(forms.ModelForm):
  
      class Meta:
          model = Blog
          fields = ('title', 'description',)
  
  class SignUpForm(UserCreationForm):
      username = forms.CharField(max_length=30)
      first_name = forms.CharField(max_length=150) #, help_text='First Name')
      last_name = forms.CharField(max_length=150) #, help_text='Last Name')
      email = forms.EmailField(max_length=200, help_text='Required. Enter a valid email address')
  
      def clean_username(self):
          nom_de_guerre=self.cleaned_data['username']
          if User.objects.filter(username=nom_de_guerre).exists():
              raise forms.ValidationError('User with this name is already exists!')
          return nom_de_guerre
  
      def clean_email(self):
          addresse_de_mail=self.cleaned_data['email']
          if User.object.filter(email=addresse_de_mail).exists():
              raise forms.ValidationError('User with this email is already exists!')
          return addresse_de_mail
  
      class Meta:
          model = User
          fields = ('username', 'email', 'first_name', 'last_name',)
  
  </code></pre>
  <p>Here is my view where I use this form:</p>
  <pre><code>    if request.method == 'POST':
          form = SignUpForm(request.POST)
          if form.is_valid():
              user = form.save(commit=False)
              user.is_active = False
              user.save()
              current_site = get_current_site(request)
              subject = '''Activate your Bard's Lair Account'''
              message = render_to_string('blog/activation_request.html', {
                  'user': user,
                  'domain': current_site.domain,
                  'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                  'token': account_activation_token.make_token(user),
              })
              user.email_user(subject, message)
              return redirect('activation_sent_view')
      else:
          form = SignUpForm()
      return render(request, 'blog/signup.html', {'form': form}
  </code></pre>
  <p>And here is my full traceback:</p>
  <pre><code>
  
  Request Method: POST
  Request URL: http://www.bards-lair.com/signup/
  
  Django Version: 4.0
  Python Version: 3.8.10
  Installed Applications:
  ['django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'blog',
   'portfolio',
   'markdownx']
  Installed Middleware:
  ['django.middleware.security.SecurityMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware']
  
  
  
  Traceback (most recent call last):
    File &quot;/home/mougrim/simple_django3_blog/django3blogenv/lib/python3.8/site-packages/django/core/handlers/exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;/home/mougrim/simple_django3_blog/django3blogenv/lib/python3.8/site-packages/django/core/handlers/base.py&quot;, line 181, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;/home/mougrim/simple_django3_blog/blog/views.py&quot;, line 56, in user_signup
      if form.is_valid():
    File &quot;/home/mougrim/simple_django3_blog/django3blogenv/lib/python3.8/site-packages/django/forms/forms.py&quot;, line 190, in is_valid
      return self.is_bound and not self.errors
    File &quot;/home/mougrim/simple_django3_blog/django3blogenv/lib/python3.8/site-packages/django/forms/forms.py&quot;, line 185, in errors
      self.full_clean()
    File &quot;/home/mougrim/simple_django3_blog/django3blogenv/lib/python3.8/site-packages/django/forms/forms.py&quot;, line 404, in full_clean
      self._clean_fields()
    File &quot;/home/mougrim/simple_django3_blog/django3blogenv/lib/python3.8/site-packages/django/forms/forms.py&quot;, line 419, in _clean_fields
      value = getattr(self, 'clean_%s' % name)()
    File &quot;/home/mougrim/simple_django3_blog/blog/forms.py&quot;, line 19, in clean_username
      def clean_username(self):
  
  Exception Type: NameError at /signup/
  Exception Value: name 'username' is not defined
  </code></pre>