开发者

Google App Engine: template not working when displaying html form

开发者 https://www.devze.com 2023-02-06 23:20 出处:网络
In my python code I have the_button = \'\'\'<form action=\"/edit\" method=\"get\" > <input type=\"submit\" name=\"edit\" value=\"Edit\" />

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:

  1. Install a more recent version of Django (Django-NonRel)
  2. 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 simple show_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 %}
0

精彩评论

暂无评论...
验证码 换一张
取 消