Shinyshell Community Forums > Coding >
Forms in Python?


[1]


June 28 02009, 23:19 GMT
Pikachu
Kelp is good!

Pikachu's avatar
Location: California
Post count: 50
us
I'm really confused on how to get data from a form when it is submitted in Python.

#!/usr/bin/env python 

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

print "<form method='post' action=''>"
print "<strong>Name:</strong> <input type='text' name='user_name' /><br />"
print "<strong>Age: </strong> <input type='text' name='user_age' /><br />"
print "<input type='submit' value='submit!' /></form>"

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

print "<form method='post' action=''>"
print "<strong>Name:</strong> <input type='text' name='user_name' /><br />"
print "<strong>Age: </strong> <input type='text' name='user_age' /><br />"
print "<input type='submit' value='submit!' /></form>"]]>


How would I get the data from the form when it's submitted?
______________________________

Linux | Chrome | Python | Chuck

June 28 02009, 23:52 GMT
lec**
Supra stultitiam

lec's avatar
Location: Varaždin, Croatia
Post count: 173
hr
There's really two ways to do this, and this is the simpler way.

Once you become more proficient in Python, you'll write a set of functions for yourself to make this easier than it is by default. For example, I wrote the functions url_param() and param() (inspired by Perl) for Shinyshell. Here's how you'd do it, though:

#!/usr/bin/python 
import cgi

forms = cgi.FieldStorage()
if forms.has_key("user_name") and forms.has_key("user_age"):
print "Name: %s <br />" % forms["user_name"].value
print "Age: %s <br /><br />" % forms["user_age"].value

print """
<form method="post" action="">
<strong>Name:</strong> <input type="text" name="user_name" /><br />
<strong>Age: </strong> <input type="text" name="user_age" /><br />
<input type="submit" value="submit!" /></form>
"""
#!/usr/bin/python
import cgi

forms = cgi.FieldStorage()
if forms.has_key("user_name") and forms.has_key("user_age"):
print "Name: %s <br />" % forms["user_name"].value
print "Age: %s <br /><br />" % forms["user_age"].value

print """
<form method="post" action="">
<strong>Name:</strong> <input type="text" name="user_name" /><br />
<strong>Age: </strong> <input type="text" name="user_age" /><br />
<input type="submit" value="submit!" /></form>
"""
]]>


It should work

June 29 02009, 00:44 GMT
Nick^
merciful justice

Nick's avatar
Location: Brisbane, Australia
Post count: 78
au
I personally love url_param() and param(), lec. They're awesome!
______________________________
Lazurane

June 30 02009, 02:01 GMT
Pikachu
Kelp is good!

Pikachu's avatar
Location: California
Post count: 50
us
Problem. I tried your code once, and it worked. I edited, didn't work. I copy/pasted the code again and uploaded, and it stopped working. What's going on?

link

Chmodded and with Linux line endings.

I might take a break from Python and work more on Perl, since I seem to like it more. Like this.
______________________________

Linux | Chrome | Python | Chuck

June 30 02009, 02:15 GMT
Nick^
merciful justice

Nick's avatar
Location: Brisbane, Australia
Post count: 78
au
You probably stuffed up the line endings, in Komodo, make sure you uncheck the 'preserve line endings' tick box and then set the line endings to UNIX.

And Perl: Yuck. Python is so much cleaner... I hate the whole $variable, it's stupid and pointless.
______________________________
Lazurane

June 30 02009, 02:18 GMT
Pikachu
Kelp is good!

Pikachu's avatar
Location: California
Post count: 50
us
Nope. Here's a screenshot: here
LF is for Unix line endings.
______________________________

Linux | Chrome | Python | Chuck

June 30 02009, 02:21 GMT
Nick^
merciful justice

Nick's avatar
Location: Brisbane, Australia
Post count: 78
au
Uh dude, your indenting the print thing. You shouldn't be.

Also, refer to the "And Perl:" part in my previous post, you probably missed it.

______________________________
Lazurane

June 30 02009, 02:24 GMT
Pikachu
Kelp is good!

Pikachu's avatar
Location: California
Post count: 50
us
Now it is, but it still won't work

#!/usr/bin/python  
import cgi

forms = cgi.FieldStorage()
if forms.has_key("user_name") and forms.has_key("user_age"):
print "Name: %s <br />" % forms["user_name"].value
print "Age: %s <br /><br />" % forms["user_age"].value

print """
<form method="post" action="">
<strong>Name:</strong> <input type="text" name="user_name" /><br />
<strong>Age: </strong> <input type="text" name="user_age" /><br />
<input type="submit" value="submit!" /></form>
"""
import cgi

forms = cgi.FieldStorage()
if forms.has_key("user_name") and forms.has_key("user_age"):
print "Name: %s <br />" % forms["user_name"].value
print "Age: %s <br /><br />" % forms["user_age"].value

print """
<form method="post" action="">
<strong>Name:</strong> <input type="text" name="user_name" /><br />
<strong>Age: </strong> <input type="text" name="user_age" /><br />
<input type="submit" value="submit!" /></form>
"""]]>


And Perl: Yes, Python is much cleaner, but I think the reason I like Perl more is that it reminds me of PHP.
______________________________

Linux | Chrome | Python | Chuck

June 30 02009, 02:27 GMT
Nick^
merciful justice

Nick's avatar
Location: Brisbane, Australia
Post count: 78
au
No, you're indenting the whole thing. It should be:

By the way, to enable code highlighting use this: (code=python) (replace the curved brackets with square ones)
print """ 
<form method="post" action="">
<strong>Name:</strong> <input type="text" name="user_name" /><br />
<strong>Age: </strong> <input type="text" name="user_age" /><br />
<input type="submit" value="submit!" /></form>
"""
print """
<form method="post" action="">
<strong>Name:</strong> <input type="text" name="user_name" /><br />
<strong>Age: </strong> <input type="text" name="user_age" /><br />
<input type="submit" value="submit!" /></form>
"""
]]>

______________________________
Lazurane

June 30 02009, 02:32 GMT
Pikachu
Kelp is good!

Pikachu's avatar
Location: California
Post count: 50
us
I didn't see the edit.

#!/usr/bin/python  
import cgi

forms = cgi.FieldStorage()
if forms.has_key("user_name") and forms.has_key("user_age"):
print "Name: %s <br />" % forms["user_name"].value
print "Age: %s <br /><br />" % forms["user_age"].value

print """
<form method="post" action="">
<strong>Name:</strong> <input type="text" name="user_name" /><br />
<strong>Age: </strong> <input type="text" name="user_age" /><br />
<input type="submit" value="submit!" /></form>
"""
#!/usr/bin/python
import cgi

forms = cgi.FieldStorage()
if forms.has_key("user_name") and forms.has_key("user_age"):
print "Name: %s <br />" % forms["user_name"].value
print "Age: %s <br /><br />" % forms["user_age"].value

print """
<form method="post" action="">
<strong>Name:</strong> <input type="text" name="user_name" /><br />
<strong>Age: </strong> <input type="text" name="user_age" /><br />
<input type="submit" value="submit!" /></form>
"""]]>

Still not working. UNIX line endings and chmod 755.
______________________________

Linux | Chrome | Python | Chuck

June 30 02009, 02:45 GMT
Nick^
merciful justice

Nick's avatar
Location: Brisbane, Australia
Post count: 78
au
What about adding...

print "Content-type:text/html\n" 
print "Content-type:text/html\n"
]]>

______________________________
Lazurane

June 30 02009, 02:57 GMT
Pikachu
Kelp is good!

Pikachu's avatar
Location: California
Post count: 50
us
The form appears now, but nothing happens when you submit it.
try if you want

Ugh, I give up. I'll come back to Python when I buy a Python for Dummies book or extensive tutorial online (other than Think Python because that didn't teach me anything).

Back to Perl.
______________________________

Linux | Chrome | Python | Chuck

June 30 02009, 04:28 GMT
Nick^
merciful justice

Nick's avatar
Location: Brisbane, Australia
Post count: 78
au
Think Python is an awesome resource, you shouldn't need anything other than it. It's sad that you've given up so easily, new languages are hard to learn and you have to stick with it to learn it. The only reason Perl is "so easy" for you is because it's so similar to PHP - and that isn't necessarily a good thing.

______________________________
Lazurane

June 30 02009, 05:01 GMT
Pikachu
Kelp is good!

Pikachu's avatar
Location: California
Post count: 50
us
I think I'll tackle Think Python again. I'm going to the library sometime this week to check out some Python books. The Python fire in my heart has been lit again!

Python for the win!

EDIT: downloaded and installed the Python interpreter on my computer (that doesn't have internet.)
______________________________

Linux | Chrome | Python | Chuck

June 30 02009, 09:51 GMT
lec**
Supra stultitiam

lec's avatar
Location: Varaždin, Croatia
Post count: 173
hr
Sorry for forgetting to print the Content-Type header, I've been doing a lot of desktop python programming recently, so I kind of forgot :o

Anyway, that code should work. I was confused when it didn't work for you, so I uploaded it, and it works.

I do not really know why it doesn't work for you, hmmm.

June 30 02009, 17:17 GMT
Pikachu
Kelp is good!

Pikachu's avatar
Location: California
Post count: 50
us
I figured out why it was not working. I had put the Content-type header before the form when it should have been right after the import CGI.

Now it works!

You can shoot me later.
______________________________

Linux | Chrome | Python | Chuck


[1]



Forum Information
  Currently Active Members [detailed] (0 members and ? guests)
-
Forum Statistics
Topics: 0, Posts: 0, Members: 108.
Welcome to our newest member, adamthephantump
Most members online was 5, on August 28 2009, at 21:49:28.
Legend
    Forums with unread topics in them are indicated by a strong yellow colouring around the forum icon.
    Forums with no unread topics have the standard pale yellow colouring around the forum icon.
    Forums with a blue arrow icon will redirect you to a non-forum page.
    Locked forums have a little padlock by their icon. You won't be able to post in these forums.
Shinyshell Home | Contact | Staff List | Archive | Top 

Conventional Login

Don't have an account? You may want to create one.

OpenID Login
OpenID login and registration is usable, but not finished.
What is OpenID?
Search

(advanced search)
Site Stats
  Total members: 108
  Latest member: adamthephantump
  Members currently online: 0
  Most online: 5 - Aug 28, 2009 (21:49)
  Front page hits: 88114
Developer info
  Site version: 3.5 Alpha
  16 queries - 9 templates
Under the Spotlight
Collide Site
Collide make fabulously dreamy electronic-industrial music, they're one of my favourite bands! Give them a chance to take control of your life - myspace | youtube - "Euphoria".

Collide Site - Hits: 4924

5/5 (2) | Rate this site?