在python 2.7中编写CGI脚本时,我一直在为get方法使用URL解析器。向前迈进我试图让我的HTML表单预先填充任何内容(如果网页刚刚打开)或用户之前输入的内容。脚本链接到自身,所以一切都被正确调用,我似乎无法得到我想要它们的python变量。
printContent-Type:text / html $ b print< html> < head> < title> CGI< / title> < / head> < body> < h3>创建HTML表单< / h3> < p>请随机输入以下内容...< / p> print'< form action =proj1.cgimethod =get>' print'< input type = checkboxname =cb1value =1/>' if f: print'< input type =textname =text1value ='+ html_encode var ['text1'])+'/>' else: print'< input type =textname =text1/>' 打印 < input type =submit/> < / form> < h3>这是您的输出:< / h3> < p> print'用户文本:%s< br />'%(html_encode(var ['text1'])) 'cb1'not在var.keys()中: print'复选框没有被检查:(< br />' else: print'复选框被选中了! />' print< / p> < br />< br /> < / body> < / html>我知道我的格式很糟糕,但我可以担心它正在工作,那些if语句只是检查数据并根据这些数据做出决定,在我的print语句中试图使用python变量时显式地出现了麻烦,我忽略了所有其他代码,以免混淆任何人,但应该知道这是坐在我的CGI脚本的底部,而不是一个独立的html文件。 答案:对我的if语句和不正确的引用使用不好的测试用例的组合。如果任何人有兴趣,我可以发布完整的代码以供参考。
I have been working on a URL parser for the get method when writing CGI scripts in python 2.7. Moving forward I am trying to have my HTML forms pre-populate with either nothing (if the webpage has been just opened) or with what the user entered previously. The script links to itself so everything is being called correctly, I just can't seem to get my python variables where I want them.
print "Content-Type: text/html" print """ <html> <head> <title> Output from CGI </title> </head> <body> <h3> Creating HTML forms </h3> <p> Please randomize input below... </p> """ print '<form action="proj1.cgi" method="get">' print '<input type="checkbox" name="cb1" value="1" />' if f: print '<input type="text" name="text1" value="'+ html_encode(var['text1'])+'" />' else: print '<input type="text" name="text1" />' print """ <input type="submit" /> </form> <h3> This was your output: </h3> <p> """ print 'User Text: %s <br />' %(html_encode(var['text1'])) if 'cb1' not in var.keys(): print 'The checkbox wasn\'t checked :(<br />' else: print 'The checkbox was checked!<br />' print """ </p> <br /><br /> </body> </html> """I know my formatting is terrible, but I can worry about that once it is working. Those if statements are just checking for data and making decisions based on said data. the trouble explicitly comes in when trying to use python variables in my print statements. I have left off all the other code as not to confuse anyone, but it should be known this is sitting at the bottom of my CGI script rather than being an independent html file.
解决方案Found the answer. A combination of using a bad test case for my if statements as well as incorrect quoting. If anyone is interested I can post the full code up for reference.
更多推荐
在python CGI中预填充HTML表单
发布评论