1

我正在尝试在 Google App Engine 应用程序中创建一个表,其中表中的背景颜色会根据输入定期更改。有谁知道如何做到这一点?这是我的代码:

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

例如,%( ) 中的红色、绿色...颜色将是会发生变化的变量,因此在某一时刻它们都可能是红色或蓝色和黄色。

4

1 回答 1

2

这种类型的字符串格式已被弃用。请.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 模板。

于 2013-03-13T22:26:03.163 回答