Python HTML格式问题

0 投票
2 回答
2008 浏览
提问于 2025-04-18 05:02

我现在正在开发一个应用程序,通过网页向用户提供各种传感器数据。这个应用使用Python-CGI动态生成HTML和JavaScript代码。

在生成网页之前,Python脚本会用str.format()方法把一些数据插入到HTML中。

为了简单起见,我省略了一些代码。

page_source = """   
<!DOCTYPE html>
    <html>

    ...

            function print_array(code)
            {
                switch (code)
                {
                   case '001':
                       SetValueVisibilty('Buttonspan', {0})
                       break
                   case '002':
                          SetValueVisibilty('Ultraspan', {1})
                       break
                   case '003':  
                          SetValueVisibilty('Gasspan', {2})
                       break;
                   case '004':  
                          SetValueVisibilty('Vib1span', {3}) 
                       break
                   case '005': 
                          SetValueVisibilty('Vib2span', {4}) 
                       break
                   case '006':   
                       SetValueVisibilty('Soundspan', {5})      
                       break
                   case '007': 
                       SetValueVisibilty('Lightspan', {6})   
                       break
                   default: 
                       alert('No Sensor');
                       break
                }                
            }

            </script>                            
            <center>
            <ul id='Sensors'>   
            <li id='Button'>          
            <INPUT type='button' value='Button' onMouseOver='return print_array('001')' onMouseOut='clearspan('Buttonspan')'/>
            <span class='invisiblespans' id='Buttonspan'></span>              
            </li>      

        ...

    </body>
    </html>""".format(get_five_entries("001"),get_five_entries("002"),get_five_entries("003"),get_five_entries("004"),get_five_entries("005"),get_five_entries("006"),get_five_entries("007"))

 page_source = """  
<!DOCTYPE html>
    <html>

    ...

            function print_array(code)
            {
                switch (code)
                {
                   case '001':
                       SetValueVisibilty('Buttonspan', {0})
                       break
                   case '002':
                          SetValueVisibilty('Ultraspan', {1})
                       break
                   case '003':  
                          SetValueVisibilty('Gasspan', {2})
                       break;
                   case '004':  
                          SetValueVisibilty('Vib1span', {3}) 
                       break
                   case '005': 
                          SetValueVisibilty('Vib2span', {4}) 
                       break
                   case '006':   
                       SetValueVisibilty('Soundspan', {5})      
                       break
                   case '007': 
                       SetValueVisibilty('Lightspan', {6})   
                       break
                   default: 
                       alert('No Sensor');
                       break
                }                
            }

            </script>                            
            <center>
            <ul id='Sensors'>   
            <li id='Button'>          
            <INPUT type='button' value='Button' onMouseOver='return print_array('001')' onMouseOut='clearspan('Buttonspan')'/>
            <span class='invisiblespans' id='Buttonspan'></span>              
            </li>      

        ...

    </body>
    </html>""".format(get_five_entries("001"),get_five_entries("002"),get_five_entries("003"),get_five_entries("004"),get_five_entries("005"),get_five_entries("006"),get_five_entries("007"))

这个函数get_five_entries()会返回一个字符串。

我在apache服务器和命令行界面上都试过,但总是出现一个KeyError错误:

KeyError: '\n margin'

我觉得这可能是因为引号用错了,所以我把所有的引号都改成了单引号,但还是没成功。

有没有人能给我一点提示来解决这个问题呢?

UPDATE:
def get_five_entries(column_code):

statusCodes = {'001':'Button', '002':'Ultra Sonic Sensor', '003':'Gas Sensor',                                
                '004':'Vibration Sensor 1','005':'Vibration Sensor 2', '006':'Sound Sensor', '007':'Light Sensor'}    

if column_code in statusCodes:   
...
   curs.execute("SELECT * FROM Log WHERE Code=:Code",{"Code":column_code}) 

2 个回答

2

我在用Python的SMTP发送邮件时,遇到了同样的错误,那个错误是在我使用HTML模板的时候。于是我尝试了两种方法。

  1. 对每个传入模板的变量使用双{{}}。
 template = """ <html>
<head> </head>
<body>
<p> Name of Criminal is {{x}} and the crime is {{y}}
</body>
 </html> """.format(x="Laden",y="Bombing in USA")
  1. 在标签结束后使用字符串拼接。
 template = """ <html>
<head> </head> """ + """
<body>
<p> Name of Criminal is {{x}} and the crime is {{y}}
</body>
 </html> """.format(x="Laden",y="Bombing in USA")

第二种方法对我有效。在这个例子中,我随便想了些模板来解释,我可不是在和FBI合作哦!

3

正如评论所说,代码中并没有出现 margin,所以这个错误是从别的地方来的。不过我猜这和同一个错误有关:{}format 的分隔符。当你有一个被 {} 包围的 JS 或 CSS 代码块时,format 会把它当作占位符,这可不是你想要的。

所以,在你真正需要用到大括号的地方,要把它们写成两个,这样就能避免被 format 解析了。也就是说:

function print_array(code)
{{
    switch (code)
    {{
        ...
    }}
}}

撰写回答