在python CGI中预填充HTML表单

编程入门 行业动态 更新时间:2024-10-25 08:16:38
本文介绍了在python CGI中预填充HTML表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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表单

本文发布于:2023-08-05 20:55:26,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   python   CGI   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!