2013-03-13 163 views
1

我試圖在Google App Engine應用程序中創建一個表格,表格中的背景顏色會根據輸入定期更改。有誰知道如何做到這一點? 這裏是我的代碼:Google App Engine Python HTML表

self.response.out.write(""" 
     <img src="/images/resistor.png" width = "150"> 
     <table border = "1"> 
     <tr height="150" > 
     <td bgcolor="%s" width="35"> </td> <td bgcolor="%s" width="35"> </td> <td bgcolor="%s" width="35"> </td> <td bgcolor="%s" width="35"> </td> %(Red,Blue,Black,Green) 
     </tr> 
     </table> 
      <form action="/sign" method="post"> 
      <div><textarea name="content" rows="3" cols="60"></textarea></div> 
      <div><input type="submit" value="Sign Guestbook"></div> 
      </form> """) 
    self.response.out.write('</pre></body></html>') 

例如紅色,綠色......在顏色%()將變量將在一個點,從而改變它們都可以是紅色或藍色和黃色。

回答

2

That type of string-formatting is deprecated。請在新代碼中使用.format()方法。例如:

self.response.out.write(""" 
    <img src="/images/resistor.png" width = "150"> 
    <table border = "1"> 
     <tr height="150" > 
     <td bgcolor="{}" width="35"> </td> 
     <td bgcolor="{}" width="35"> </td> 
     <td bgcolor="{}" width="35"> </td> 
     <td bgcolor="{}" width="35"> </td> 
     </tr> 
    </table> 
    <form action="/sign" method="post"> 
     <div><textarea name="content" rows="3" cols="60"></textarea></div> 
     <div><input type="submit" value="Sign Guestbook"></div> 
    </form> """.format(('Red','Blue','Black','Green'))) 
self.response.out.write('</pre></body></html>') 

而對於超出基本的任何東西,請看使用模板。模板系統的例子是Jinja2和Django模板。

+0

謝謝你的幫助,但我得到錯誤文件「/Users/ben/Desktop/theresistorpage/main.py」,第116行,後 「」「.format(('Red','藍色','黑色','綠色'))) IndexError:元組索引超出範圍 – ben 2013-03-13 23:36:23

+1

沒關係,我不小心加了一個括號,謝謝!!! – ben 2013-03-13 23:37:53

+0

非常歡迎。 – bernie 2013-03-13 23:39:12