title: How can one create a custom "as_table()" method for forms?
body: <p>I have a relatively complicated form that's used in multiple places on my website (in fact, it's a form from which many other form classes inherit). In the templates, the inherited part of this form is always formatted identically—but that formatting is somehwat involved; each field is rendered and positioned manually in the template.</p>
  
  <p>This means that every template which displays this form has a lot of identical HTML markup that renders the form appropriately. </p>
  
  <p>I would like to create a custom output that can be called, similar to the <code>as_table()</code> methods. I'm aware that one can override the <code>normal_row</code>, <code>error_row</code>, etc. attributes—but the formatting of this form goes beyond that (for example, three of the form's five fields should be printed side-by-side, with a combined title). All of the tutorials/answered-questions I've seen either refer to overriding the above-mentioned attributes, or give instructions on how to manually render forms. </p>
  
  <p>Originally, I was thinking something like this:</p>
  
  <pre><code>Class StrangeForm(form.Forms):
  ....
      def as_table_custom():
          html_string = "\
             &lt;tr&gt;&lt;td&gt;Title 1:&lt;/td&gt;&lt;td&gt;self.fields['field1']&lt;/td&gt;&lt;tr&gt;\
             &lt;tr&gt;&lt;td&gt;Title 2:&lt;/td&gt;&lt;td&gt;self.fields['field2']&lt;/td&gt;&lt;tr&gt;\
             &lt;tr&gt;&lt;td&gt;Titles 3, 4, 5:&lt;/td&gt;&lt;td&gt;self.fields['field3']\
             &lt;/td&gt;&lt;td&gt;self.fields['field4']&lt;/td&gt;&lt;td&gt;self.fields['field5']&lt;/td&gt;&lt;/tr&gt;\
          "
      return html_string
  </code></pre>
  
  <p>But, after reading through the <code>_html_output()</code> and <code>as_table()</code> methods of <a href="https://github.com/django/django/blob/master/django/forms/forms.py" rel="nofollow noreferrer">Django's forms.py file</a>, it doesn't look like it'll be that easy. If I write this from scratch, have to somehow account for errors, help text, etc. I think. </p>
  
  <p>Is there an easy way to override something such that the form's HTML output can be defined like above? Or do I have to re-write things from scratch. If the latter, how can I account for all of the things I need to account for? </p>