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 に答える