2011-01-26 28 views
1

當在我的Python代碼模板不工作我有谷歌應用程序引擎:顯示HTML表單

the_button = '''<form action="/edit" method="get" > 
        <input type="submit" name="edit" value="Edit" /> 
        </form>''' 

    template_values = { 'the_button':the_button } 
    path = os.path.join(os.path.dirname(__file__), 'the.html') 
    self.response.out.write(template.render(path, template_values)) 

的問題是,當我在頁面中看到它會顯示按鈕,但不可點擊,然後當我查看html的頁面源代碼,它不顯示代碼。

可能是什麼問題?

謝謝!

+1

在`the.html`模板是什麼? – 2011-01-26 04:19:42

+0

在模板{{the_button}} – Peter 2011-01-26 06:24:31

回答

2

Webapp可能是逃避你的原始html。
嘗試添加一個safe過濾器是這樣的:

{{ the_button|safe }} 

從Django的documentation

safe
Marks a string as not requiring further HTML escaping prior to output.
When autoescaping is off, this filter has no effect.

編輯:
不幸的谷歌應用程序引擎的開箱運行的Django 0.96不具備該功能。
你有兩個選擇:

  1. 安裝較新版本的Django的(Django-NonRel
  2. 問自己,爲什麼你需要通過一個原始的HTML風險從控制器到視圖片段。
    我會將該按鈕移動到視圖中,允許控制器使用簡單的show_button布爾變量顯示或不顯示。

在控制器:

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)) 

the.html

{%if show_button %} 
    <form action="/edit" method="get" > 
        <input type="submit" name="edit" value="Edit" /> 
        </form> 
{% endif %}