Skip to content Skip to sidebar Skip to footer

Django: How To Make A Form With Custom Templating?

I have a model: class Setting(models.Model): class Meta: abstract = True name = models.CharField(max_length=120, primary_key=True) description = models.CharFi

Solution 1:

<formaction="/contact/"method="post">
    {% for field in form %}
        <divclass="fieldWrapper">
            {{ field.errors }}
            {{ field.label_tag }}: {{ field }}
        </div>
    {% endfor %}
    <p><inputtype="submit"value="Send message" /></p></form>

You can find the complete documentation here: http://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template

Solution 2:

I don't think you need a formset here. Take a look here if you want a custom template for one view. If you want to create your own {{ form.as_foobar }}, just subclass forms.Form, something like this:

classMyForm(forms.Form):
  defas_foobar(self):
    return self._html_output(
      normal_row = u'%(label)s %(field)s%(help_text)s',
      error_row = u'%s',
      row_ender = '',
      help_text_html = u' %s',
      errors_on_separate_row = False)

and just use it in your forms.py:

classContactForm(MyForm):
  # ..

Solution 3:

For whoever needs the <table> version of jbcurtin's answer:

<formmethod="post">{% csrf_token %}
  <table>
    {% for field in form %}
      <tr><th>{{field.label_tag}}</th><td>
          {{ field.errors }}
          {{ field }}
        </td></tr>
    {% endfor %}
  </table><hr/><inputtype="submit"value="Conferma" /></form>

Solution 4:

Looks like you might be interested in django-floppyforms (docs), which gives you much more control over field rendering.

Post a Comment for "Django: How To Make A Form With Custom Templating?"