Python CGI redirect

Python CGI redirect

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

Hi

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.

Thanks
rhyswilson92
 
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.
setrofim
 
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

<html>
    <head>
        <title>{TITLE}</title>
        <script></script>
    </head>
<body>

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

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

</body>
</html>
""".format(
        TITLE='my title',
        FORM=form
    )
print(html)

where obviously this scripts name is test1.py

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
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1500
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