title: Django ajax form - redirect to main page after form validation
body: <p>I have a <code>sidenav_form</code> that is submitted using an ajax post to a view called <code>form_validation</code>. The validation errors for <strong>empty fields</strong> or <strong>invalid email</strong> are shown correctly, since when the form is submitted and passed to the view, the view renders the form displaying the errors. Here is the console output:</p>
  
  <pre><code>&lt;ul class="errorlist"&gt;&lt;li&gt;email&lt;ul class="errorlist"&gt;&lt;li&gt;Required Field&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
  [08/Feb/2020 13:26:37] "POST /patients/validate_form/ HTTP/1.1" 200 1253
  </code></pre>
  
  <p>However, the behavior when passing the validator for <strong>repeated email</strong> is different. The error is sent to the request as before, but the user an't see it since it seems that the request redirects to another view... </p>
  
  <pre><code>&lt;ul class="errorlist"&gt;&lt;li&gt;email&lt;ul class="errorlist"&gt;&lt;li&gt;This email has already been taken.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
  [08/Feb/2020 13:26:40] "POST /patient/ HTTP/1.1" 200 7769
  [08/Feb/2020 13:26:40] "POST /patients/validate_form/ HTTP/1.1" 200 1279
  </code></pre>
  
  <p>The <code>sidenav_form.html</code> is the following:</p>
  
  <pre><code>{% load i18n %}
  {% load crispy_forms_tags %}
  
  &lt;!-- sideNav --&gt;
  &lt;div class="inside_sidenav"&gt;
          &lt;form class="form" id="form_patient"&gt;
              {% csrf_token %}
              {{ form.as_p}}
              &lt;/div&gt;
              &lt;div class="form-actions"&gt;
                  &lt;button type="submit" class="btn btn-primary" id="sidenav-btn-submit" formmethod="post"&gt;{% trans 'Guardar'%}
                  &lt;/button&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  
  
  &lt;script&gt;
      $(function () {
          // SUBMIT DATA
          $('#sidenav-btn-submit').click(function () {
              console.log('noinstance')
              var url = "{% url 'patients:validate_form' %}";
              var formb = $('#form_patient');
              console.log(formb.serialize());
              $.ajax({
                  url: url,
                  data: formb.serialize(),
                  type: "POST",
                  headers: { "X-CSRFToken": '{{ csrf_token }}' },
                  dataType: 'json',
                  success: function (data) {
                      $('#mySidenav').html(data);
                      openNav();
                  },
                  error: function (data) {
                      $('#mySidenav').html(data.responseText);
                      console.log('no ajax2');
                  },
  
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><code>form_validation</code> view checks if the form is correct or not. It looks as follows:</p>
  
  <pre><code>def validate_form(request, patient_id=None, user_id=None):
      form_initial_data = request.POST
      if request.method == 'POST':
          print(form_initial_data)
          form = UserForm(form_initial_data)
          if form.is_valid():
              print('Form2 is valid')
              # DO SOMETHING
      print(form.errors)
      return render(request, 'patients/sidenav_form.html', {'form1': form})
  </code></pre>
  
  <p>the <code>form</code> has the following validator:</p>
  
  <pre><code>def clean_email(self):
          email = self.cleaned_data["email"]
  
          if not self.instance.pk:
              try:
                  User.objects.get(email=email)
              except User.DoesNotExist:
                  return email
              print('Repeated email')
              raise ValidationError(_("This email has already been taken."))
          return email
  </code></pre>
  
  <p>Do you have any idea why errors related to <strong>empty fields</strong> or <strong>invalid email</strong> renders correctly the form errors but <strong>repeated email</strong> redirects to another page?</p>