Andrew Boissonnault
Andrew Boissonnault

Reputation: 101

Google App Engine render and Django forms

I'm trying to use Django forms in my Google App Engine project, using the template system. However I can't figure out how to get forms to be rendered properly. When calling form.as_p () from python code I get the expected html,

<p><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>

yet when calling form.as_p from the template I instead get

&lt;p&gt;&lt;label for=&quot;id_name&quot;&gt;Name:&lt;/label&gt; &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;id_name&quot; /&gt;&lt;/p&gt;

Here is my render method

def render(self, name, **data):
    """Render a template"""
    if not data:
        data = {}
    self.response.out.write(template.render(
        os.path.join(
            os.path.dirname(__file__), 'templates', name + '.html'),
        data))

Render method being called:

form = PostForm()
self.render('toplevel', parent=user, items=user.establishments(), form=form)

And finally the template:

{% block content %}

    {{ form.as_p }}

{% endblock content %}

Upvotes: 1

Views: 230

Answers (1)

Aaron Hampton
Aaron Hampton

Reputation: 894

Change your form in the template to:

{% block content %}

    {{ form.as_p|safe }}

{% endblock content %}

That should prevent the HTML in as_p from being escaped. See: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe

Upvotes: 1

Related Questions