Hi,
I have a page with 3 buttons. When I press one, I want the page to tell me
which button I pressed.
My problem is taking the button action, do something with it and return the
result to the page with the buttons.
I've read the tutorials, also looked for examples in templetor, all forms
there seem to handle a login routine or write the response to a database..
There must be something I don't get about the form routine as I built it.
*The script:*
#===============================================================================
# Imports
#===============================================================================
import web
import os
from web import form
#===============================================================================
# Start webPy environment
#===============================================================================
urls = ('/', 'index',
'/images/(.*)', 'images'
)
render = web.template.render('templates/')
button_resp = "temp"
#===============================================================================
# Menu buttons
#===============================================================================
my_form = form.Form(
form.Button("btn", id="Search planet", value="ipfact", html="Find Target",
class_="ipfact"),
form.Button("btn", id="Lock on Target", value="lockta", html="Select
planet to target", class_="lockta"),
form.Button("btn", id="Destroy all humans", value="deshum", html="Destroy
all humans", class_="deshum")
)
#===============================================================================
# Classes
#===============================================================================
class index:
def GET(self):
form = my_form()
return render.index(form, button_resp)
def POST(self):
form = my_form()
userData = web.input()
if not form.validates():
return render.formtest(form)
else:
# Determine which colour LedBorg should display
if userData.btn == "ipfact":
button_resp = "Find Target"
print "ipfact"
elif userData.btn == "lockta":
button_resp = "Select planet to target"
print "lockta"
elif userData.btn == "deshum":
button_resp = "Destroy all humans"
print "deshum"
else:
button_resp = "Do nothing else - assume something fishy is
going on..."
print "nothing"
# do something cool with the menu options
# or nothing?!
# reload the web form ready for the next user input
raise web.seeother('/')
class images:
def GET(self,name):
ext = name.split(".")[-1] # Gather extension
cType = {
"png":"images/png",
"jpg":"images/jpeg",
"gif":"images/gif",
"ico":"images/x-icon" }
if name in os.listdir('images'): # Security
web.header("Content-Type", cType[ext]) # Set the Header
return open('images/%s'%name,"rb").read() # Notice 'rb' for
reading images
else:
raise web.notfound()
#===============================================================================
# Run it!
#===============================================================================
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
*The index.html template file:*
$def with (form, button_resp)
<!doctype html>
<html>
<head
<link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon">
<link rel="icon" href="/images/favicon.ico" type="image/x-icon">
</head>
<center>
<img src="/images/deathstar.jpg" width="256" height="256" >
<br>
<b>Welcome aboard!</b>
<br>
$button_resp
</center>
<form name="main" method="post">
$:form.render()
</form>
</html>
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/webpy.
For more options, visit https://groups.google.com/groups/opt_out.