سؤال

I'm currently developing an application which serves all kind of sensor data to the user via a web page. Python-CGI is used to create the HTML and JavaScript code dynamically.

Before creating it the python script uses str.format() to insert some data into the html.

For the sake of simplicity I've omitted some code.

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

The function get_five_entries() returns a string.

I tried this both on a apache server and on the cli, but I'm always getting a KeyError:

KeyError: '\n margin'

I thought this could be caused by some misuse of quotes, so I've changed them all to single quotes, but still no success.

Could somebody please give me a hint to solve this?! 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}) 
هل كانت مفيدة؟

المحلول

Like the comments said, there is no margin anywhere in that code, so that error is coming from somewhere else. But I'm guessing it's all related to the same error: { and } are delimiters for format. So when you have a JS or CSS block surrounded by { and }, format sees that as a placeholder - which isn't what you want.

So, anywhere you actually want real braces, double them up to escape them from format parsing. i.e.:

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

نصائح أخرى

i was getting same error in my code where i used html templates for sending emails using python smtp. I tried two things.

  1. use double {{}} for every variable you pass in template.
 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. use string concat after the tag ends.
 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")

Second one worked for me. In example i took random templates to explain which came in my mind , i don't work with FBI!!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top