title: Django: Giving validation error feedback on a form after a post redirect get
body: <p>I have a good bit of experience in non-Django web development, but I am struggling to find a good practice for handling what to do when the user enters invalid data in their form and I want to re-display the form with their submitted data and the form errors displayed as well.  I have a simple form with three fields and this is how I finally made it work.</p>
  
  <pre><code>def get(self, request) :
      # Check if we have been redirected...
      redirect_html = request.session.pop('form_error_html', False)
      if redirect_html : return HttpResponse(redirect_html)
  
      old_data = {'title': 'SakaiCar', 'mileage' : 42, 
          'purchase_date': '2018-08-14' }
      form = BasicForm(initial=old_data)
      ctx = {'form' : form}
      return render(request, 'form.html', ctx)
  
  def post(self, request) :
      form = BasicForm(request.POST)
      if not form.is_valid() :
          ctx = {'form' : form}
          html = render_to_string('form.html', ctx, request=request)
          request.session['form_error_html'] = html
          return redirect(request.path)
  
      # Do something with the valid data..
      return redirect('/')
  </code></pre>
  
  <p>My template is really basic (I like this simplicity):</p>
  
  <pre><code>&lt;p&gt;
    &lt;form action="" method="post"&gt;
      {% csrf_token %}
      &lt;table&gt;
      {{ form.as_table }}
      &lt;/table&gt;
      &lt;input type="submit" value="Submit"&gt;
    &lt;/form&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>Now this approach kind of weirds me out because I am sending the entire rendered HTML page through the session from the <code>post()</code> to the <code>get()</code>.  But I can't send the <code>form</code> with the errors in place through the session back to the the <code>get()</code> (that would be prettier) because it won't serialize - you get "Object of type 'BasicForm' is not JSON serializable". </p>
  
  <p>I have done this a different way where I extract the errors form the <code>form</code> object into a <code>list</code> and then pass my own list of errors from the <code>post()</code> to the redirected <code>get()</code> and then alter <code>form.html</code> to display errors.</p>
  
  <pre><code>{% if errors %}
      {% for error in errors %}
      &lt;p style="color:red"&gt;Error in {{ error.label }}: {{ error.message }}&lt;/p&gt;
      {% endfor %}
  {% endif %}
  </code></pre>
  
  <p>I have not included all the Python code to make this work - but you get the idea. 
   This feels more elegant because I am not putting a blob of HTML into the session, but then the errors display in a way other than the normal Django forms way.  And if I were using crispy forms - then all that crispy UI goodness would not come into play.</p>
  
  <p>I even thought about pulling out the errors in the <code>post()</code> code and passing them to the <code>get()</code> through the session and then inserting them into the <code>form</code> object in the <code>get()</code> prior to <code>render()</code> - that would feel more elegant too.  If I get bored I might try to dig through the forms structure and implement this as well.</p>
  
  <p>I just cannot believe that with Django having so mush awesome built-in magic - that I can't just say something like <code>return form.post_redirect_get()</code>  in the <code>not form.is_valid</code> code.</p>
  
  <p>I want something that is a replicable pattern that is easily explained and used the Django UI elements as much as possible. </p>