Google App Engine: template not working when displaying html form
In my python code I have
the_button = '''<form action="/edit" method="get" >
<input type="submit" name="edit" value="Edit" />
</form>'''
template_values = { 'the_button':the_button }
p开发者_运维知识库ath = os.path.join(os.path.dirname(__file__), 'the.html')
self.response.out.write(template.render(path, template_values))
The problem is that when I see in the page it will show the button but non-clickable, then when I view the page source of the html it doesn't show the code.
What could be the problem?
Thanks!
Webapp is probably escaping your raw html.
Try to add a safe
filter like this:
{{ the_button|safe }}
From Django documentation:
safe
Marks a string as not requiring further HTML escaping prior to output.
When autoescaping is off, this filter has no effect.
EDIT:
Unluckily Google App Engine out of the box runs Django 0.96 that does not have that feature.
You have two options:
- Install a more recent version of Django (Django-NonRel)
- Ask yourself why you need to pass a raw risky html snippet from the controller to the view.
I would move that button to the view instead, allowing the controller to show it or not using a simpleshow_button
boolean variable.
In the controller:
show_button = true
template_values = { 'show_button':show_button }
path = os.path.join(os.path.dirname(__file__), 'the.html')
self.response.out.write(template.render(path, template_values))
In the.html
{%if show_button %}
<form action="/edit" method="get" >
<input type="submit" name="edit" value="Edit" />
</form>
{% endif %}
精彩评论