egoz
egoz

Reputation: 364

How to manually render a part of a django form

I actually render a django form with the following code into template.html :

<form action="{% url "demande_create_form" %}" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Envoyer" />
</form>

One of the field is rendered with a checkbox widget, with a lot of values, so I would like to put them in a table.

Is there a way to do this without manually render the whole form ?

Upvotes: 2

Views: 7314

Answers (1)

Raphael Laurent
Raphael Laurent

Reputation: 1951

You can simply use following lines (with their corresponding errors) :

<form action="{% url "demande_create_form" %}" method="post">{% csrf_token %}
  <li>
    {{ form.nameOfField1 }}
    {{ form.nameOfField1.errors }}
  </li>
  <li>
    {{ form.nameOfField2 }}
    {{ form.nameOfField2.errors }}
  </li>
  <input type="submit" value="Envoyer" />
</form>

Here is the doc for 1.6 : https://docs.djangoproject.com/en/1.6/topics/forms/#customizing-the-form-template

Also, as you have many fields, you can use some code like this :

<form action="{% url "demande_create_form" %}" method="post">{% csrf_token %}
  {% for field in form %}
    <li>
        {{ field.errors }}
        {{ field.label_tag }} {{ field }}
    </li>
  {% endfor %}
  <input type="submit" value="Envoyer" />
</form>

Of course, you can use anything else than <li>.

Upvotes: 5

Related Questions