我有一个带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()
index.html模板文件:
$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>
答案 0 :(得分:1)
您必须讨论异步请求,其中网页可以处理请求而无需刷新页面。不像登录页面那样的web.py示例。
易。使用jQuery完成工作。 documentation足以让您入门。但在这里,我将举一个例子。
将此代码段添加到.js文件中:
$.ajax({
url: '/',
type: 'POST',
data: {
params: text
}).done(function(){
/* Do something */
});