REA_ANDREW
REA_ANDREW

Reputation: 10764

How to use a Django include tag for a separate HTML template?

I am trying to use the include Django template tag, and inside it I reference a template which handles the format of the form. When I reference this though inside my template it outputs each of the dynamic parts, each character per new line, it is really strange. For example here is a snippet of the output:

<form action="/admin/events/create_submit/" method="post">

    <div class="fieldWrapper">

        : &lt;
    </div>

    <div class="fieldWrapper">

        : l
    </div>

    <div class="fieldWrapper">


        : i
    </div>

    <div class="fieldWrapper">

        : &gt;
    </div>

...

EXPECTED OUTPUT

<form action="/admin/events/create_submit/" method="post">

        <div class="fieldWrapper">
           <li><label>field</label><input type="text" /></li>
        </div>

...

I realise the markup on the li inside the div is incorrect but I am trying to understand why the html is being encoded and each character split into a new line inside the template div and prefixed with the colon ":"

The template which I am trying to render is this:

<form action="{{action}}" method="post">
{% for field in form %}
    <div class="fieldWrapper">
        {{ field.errors }}
        {{ field.label_tag }}: {{ field }}
    </div>
{% endfor %}
    <p><input type="submit" value="Submit" /></p>
</form>

And I reference the include template like this:

{% include "forms/form_template.html" %}

Does anyone know or could help m as to why this would be causing each dynamic piece to output per character on each line?

TIA

Andrew

Upvotes: 1

Views: 1924

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

form is probably a string rather than a Form object, and iterating over a string yields its individual characters.

Upvotes: 1

Antony Hatchkins
Antony Hatchkins

Reputation: 33974

spaceless tag

Removes whitespace between HTML tags. This includes tab characters and newlines.

Upvotes: 1

Related Questions