简体   繁体   中英

Passing variable with Python CGI

I am using Python CGI to handle a simple web interface that has the following function:

User enters an ID in a form-field, clicks submit and gets a list of variables back from a dictonary containing lists. With a textfield the user can enter another item, click submit to append the value to the list corresponding with the previously entered 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. But the value of the ID is lost then. 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?

I have to use python & cgi for this project, without any additional framework.

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">以第二种形式传递回,以确保以后续的插入形式传递回去。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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