title: How to get Django view to return form errors
body: <p>This is my view:</p>
  
  <pre><code>def main_page(request):
  
      if request.method == 'POST':
          form = RegistrationForm(request.POST)
          if form.is_valid():
              user = User.objects.create_user(
                  username=form.clean_data['username'],
                  password=form.clean_data['password1'],
                  email=form.clean_data['email']
              )
          return HttpResponseRedirect('/')
      else:
          form = RegistrationForm()
          variables = {
              'form': form
          }
          return render(request, 'main_page.html', variables)
  </code></pre>
  
  <p>and this is my main_page.html:</p>
  
  <pre><code>{% if form.errors %}
      &lt;p&gt;NOT VALID&lt;/p&gt;
      {% for errors in form.errors %}
          {{ errors }}
      {% endfor %}
  {% endif %}
  &lt;form method="post" action="/"&gt;{% csrf_token %}
      &lt;p&gt;&lt;label for="id_username"&gt;Username:&lt;/label&gt;{{ form.username }}&lt;/p&gt;
      &lt;p&gt;&lt;label for="id_email"&gt;Email Address:&lt;/label&gt;{{ form.email }}&lt;/p&gt;
      &lt;p&gt;&lt;label for="id_password"&gt;Password:&lt;/label&gt;{{ form.password1 }}&lt;/p&gt;
      &lt;p&gt;&lt;label for="id_retypePassword"&gt;Retype Password:&lt;/label&gt;{{ form.password2 }}&lt;/p&gt;
      &lt;input type="hidden" name="next" /&gt;
      &lt;input type="submit" value="Register" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>When I go to the url which uses the main_page view, it just displays the form. When I submit the form with errors (with blank fields and without a proper email address) it just redirects me to the same page and doesn't display any errors. It doesn't even say "NOT VALID". </p>
  
  <p>When I change </p>
  
  <pre><code>{% if form.errors %}
  </code></pre>
  
  <p>to</p>
  
  <pre><code>{% if not form.is_valid %}
  </code></pre>
  
  <p>it always says "NOT VALID" (even if it is the first time going to the url and even if I didn't submit anything yet). </p>
  
  <p>This is my RegistrationForm:</p>
  
  <pre><code>from django import forms
  import re
  from django.contrib.auth.models import User
  from django.core.exceptions import ObjectDoesNotExist
  
  class RegistrationForm(forms.Form):
      username = forms.CharField(label='Username', max_length=30)
      email = forms.EmailField(label='Email')
      password1 = forms.CharField(label='Password', widget=forms.PasswordInput())
      password2 = forms.CharField(label='Password (Again)', widget=forms.PasswordInput())
  
      def clean_password2(self):
          if 'password1' in self.cleaned_data:
              password1 = self.cleaned_data['password1']
              password2 = self.cleaned_data['password2']
              if password1 == password2:
                  return password2
          raise forms.ValidationError('Passwords do not match.')
  
          def clean_username(self):
          username = self.cleaned_data['username']
          if not re.search(r'^\w+$', username): #checks if all the characters in username are in the regex. If they aren't, it returns None
              raise forms.ValidationError('Username can only contain alphanumeric characters and the underscore.')
          try:
              User.objects.get(username=username) #this raises an ObjectDoesNotExist exception if it doesn't find a user with that username
          except ObjectDoesNotExist:
              return username #if username doesn't exist, this is good. We can create the username
          raise forms.ValidationError('Username is already taken.')
  </code></pre>