Python 带有web.py的表单按钮例程
我有一个有3个按钮的页面。当我按下一个按钮时,我希望页面告诉我按下了哪个按钮。最后,我想从这个页面切换流程和运行脚本,并立即在按钮旁边获得反馈。就像一个控制面板 我的问题是执行按钮操作,对其执行一些操作,然后使用按钮将结果返回到页面 我已经阅读了web.py表单教程并进行了跟踪。我还查找了templetor中的示例,其中的所有表单似乎都处理登录例程或将响应写入数据库。有时,响应会显示在新页面上,但不会显示在按钮旁边的同一页面上 在我构建表单例程的过程中,一定有一些我不了解的东西 脚本:Python 带有web.py的表单按钮例程,python,python-2.7,web.py,Python,Python 2.7,Web.py,我有一个有3个按钮的页面。当我按下一个按钮时,我希望页面告诉我按下了哪个按钮。最后,我想从这个页面切换流程和运行脚本,并立即在按钮旁边获得反馈。就像一个控制面板 我的问题是执行按钮操作,对其执行一些操作,然后使用按钮将结果返回到页面 我已经阅读了web.py表单教程并进行了跟踪。我还查找了templetor中的示例,其中的所有表单似乎都处理登录例程或将响应写入数据库。有时,响应会显示在新页面上,但不会显示在按钮旁边的同一页面上 在我构建表单例程的过程中,一定有一些我不了解的东西 脚本: #===
#===============================================================================
# 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"
# 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()
$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>
index.html模板文件:
#===============================================================================
# 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"
# 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()
$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>
$def带(表单、按钮)
您一定在谈论异步请求,在异步请求中,网页可以处理请求而不刷新页面。不像web.py示例那样是登录页面
简单。对作业使用jQuery。这已经足够让你开始了。但在这里我会给你举个例子
将此代码段添加到.js文件:
$.ajax({
url: '/',
type: 'POST',
data: {
params: text
}).done(function(){
/* Do something */
});