Python CGI: How to redirect to another page after processing POST data

后端 未结 2 1183
后悔当初
后悔当初 2021-01-25 22:41

I\'m trying to write a Python script, in pyscripts/find_match.py that will process data received from a POST in an upload.php page, send it to connect.php and then redirect to a

2条回答
  •  无人共我
    2021-01-25 22:54

    I wish people didn't keep trying to write CGI in 2014.

    To redirect in a plain CGI application, you just need to output the destination next to a "Location:" header. However, you have already closed the headers and printed a blank HTML document at the top of your script. Don't do that: it's not only wrong for the redirection, it's also wrong for your alternative path, the form error, since you have already closed the HTML tags.

    Instead, start your script like this:

    # No printing at the start!
    import cgi
    ...
    
    try:
        form = cgi.FieldStorage()
        fn = form.getvalue('picture_name')
        cat_id = form.getvalue('selected')
    except KeyError:
        print "Content-type: text/html"
        print
        print "error"
    else:
        ...
        if response.status == 200:
            print "Location: response.php"
    

提交回复
热议问题