Python CGI redirect

Python CGI redirect

Postby rhyswilson92 » Thu Mar 14, 2013 2:30 pm


I am writing a small web application to control my raspberry pi.
I currently have some buttons submitting information to the program but whenever I push the submit button
the browser loads the output from the python program which is blank.
How can I code it so is that the html code sends information to the python programme
but the browser remains on the same page as the buttons?

I am new to this so any help would help.

Posts: 5
Joined: Tue Mar 12, 2013 8:47 pm

Re: Python CGI redirect

Postby setrofim » Thu Mar 14, 2013 2:41 pm

This sort of thing is done with AJAX; and you would typically use jQuery. How exactly you use it with Python will depend on how you're implementing your application (e.g. which web framework you're using). I haven't done much work with CGI, so can't give you an example; but googling "jquery with python" will turn up a bunch of relevant resources; e.g. this.
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Python CGI redirect

Postby metulburr » Thu Mar 14, 2013 4:40 pm

If it is a small script, I have always redirected the scripts back to itself. Of course with this method you have to account for both data and no data and displaying the html with the same script, but then again its a small script anyways.

this is a real raw limited example
Code: Select all
#!/usr/bin/env python3

import cgi
import cgitb; cgitb.enable()

form = cgi.FieldStorage()

html = """Content-type: text/html\n


<b>cgi.FieldStorage() is: <br></b>
{FORM} <br><br>

<form name="input" action="" method="get">
Enter something: <input type="text" name="something">
<input type="submit" value="Submit">

        TITLE='my title',

where obviously this scripts name is

So basically the form calls the same script, giving the data via url, but python has to handle when fieldStorage both has data and does not

I havent done cgi in awhile though so i might not be up to par.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
User avatar
Posts: 2120
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Return to Web Development

Who is online

Users browsing this forum: No registered users and 2 guests