简体   繁体   English

使用Python CGI传递变量

[英]Passing variable with Python CGI

I am using Python CGI to handle a simple web interface that has the following function: 我正在使用Python CGI处理具有以下功能的简单Web界面:

User enters an ID in a form-field, clicks submit and gets a list of variables back from a dictonary containing lists. 用户在表单字段中输入ID,单击“提交”,然后从包含列表的字典中获取变量列表。 With a textfield the user can enter another item, click submit to append the value to the list corresponding with the previously entered ID. 使用文本字段,用户可以输入另一个项目,单击提交以将值附加到与先前输入的ID对应的列表中。

The first submit works well, checking whether the ID exists in the dictionary and then prints the form field for adding a new item. 第一个提交工作良好,检查ID是否存在于字典中,然后打印用于添加新项目的表单字段。 But the value of the ID is lost then. 但是,ID的值随后丢失了。 What would be the best way to pass on that value? 传递该价值的最佳方法是什么?

Would posting the first value as GET and retrieving it through the URL be a solution to this or is there a better way? 将第一个值发布为GET并通过URL检索它可以解决此问题,还是有更好的方法?

I have to use python & cgi for this project, without any additional framework. 我必须为此项目使用python&cgi,而无需任何其他框架。

data = cgi.FieldStorage()

if data.has_key('ID'):
    myID = data['myID'].value
    if testme(ID): #checks if ID exists in dictionary
        printmessages(myID)
        addmessage(myID)
elif data.has_key('newitem'):
    newmitem = data['newitem'].value
    insertmessage(myID, newitem) #insert newitem to dictionary with myID

通常,原始ID将作为隐藏输入字段<input name="id" type="hidden" value="myID">以第二种形式传递回,以确保以后续的插入形式传递回去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM