1

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 %}
4

1 回答 1

1

将模板中的表单更改为:

{% block content %}

    {{ form.as_p|safe }}

{% endblock content %}

这应该可以防止 as_p 中的 HTML 被转义。请参阅:https ://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe

于 2012-10-25T12:33:22.753 回答