Reputation: 39
I'm programming a simple python script that if launched on localhost (with apache) it will generate an html page.
My script is this (test.py):
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cgitb
cgitb.enable()
import cgi
form = cgi.FieldStorage()
print "Content-type: text/html\n\n"
x="hello"
y="world"
f= open('my.html', 'r').read()
print f.format(x=val1, y=val2)
This opens an html page that has a simple Javascript in the head element:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test html</title>
<script type="text/javascript">
$(document).ready(function() {
$("#select1").change(function() {
var selectedVal = $(this).find("option:selected").val();
$("#select2 option").removeAttr("disabled").removeAttr("selected");
$("#select2 option").each(function() {
if($(this).val() != selectedVal && $(this).val() != -1)
$(this).attr("disabled","disabled").removeAttr("selected");
});
});
});
</script>
</head>
With a lot of code in the body. When I run the test.py it says: A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
/Library/WebServer/CGI-Executables/test.py in ()
181
182
184 f= open('my.html', 'r').read()
=> 185 print f.format(x=val1, y=val2)
f = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN...\n </form>\n <hr>\n </body>\n</html>', f.format = <built-in method format of str object>, val1 = 0, val2 = '','
<type 'exceptions.KeyError'>: '\n\t\t\t $("#select1")'
args = ('\n\t\t\t $("#select1")',)
message = '\n\t\t\t $("#select1")'
But if i delete the Javascript the python generates the html without problems, but i need that script.
How can i execute the script without error?
Upvotes: 0
Views: 540
Reputation: 4308
I think the problem is that format expects anything between two French braces to be replaced by one of your format strings. In your case, then, it tries to lookup
$("#select1").change(function() {
var selectedVal = $(this).find("option:selected").val();
$("#select2 option").removeAttr("disabled").removeAttr("selected");
$("#select2 option").each(function() {
if($(this).val() != selectedVal && $(this).val() != -1)
$(this).attr("disabled","disabled").removeAttr("selected");
as a key in the kwargs you passed. The solution, outlined here String format a JSON string gives KeyError is to use double braces. Your new html file then should look something like this:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test html</title>
<script type="text/javascript">
$(document).ready(function() {{
$("#select1").change(function() {{
var selectedVal = $(this).find("option:selected").val();
$("#select2 option").removeAttr("disabled").removeAttr("selected");
$("#select2 option").each(function() {{
if($(this).val() != selectedVal && $(this).val() != -1)
$(this).attr("disabled","disabled").removeAttr("selected");
}});
}});
}});
</script>
</head>
(Note the changes from '{' to '{{' and '}' to '}}'.)
Let me know if you have any follow up questions/something didn't work.
Upvotes: 1