使用 web.py 创建表单按钮程序

2024-05-19 10:09:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个有3个按钮的页面。当我按下一个按钮时,我希望页面告诉我我按下了哪个按钮。最后,我想切换进程并从这个页面运行脚本,并立即获得按钮旁边的反馈。就像控制面板。在

我的问题是执行按钮操作,用它做一些事情,然后用按钮将结果返回到页面。在

我读过网页.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()     

在索引.html模板文件:

^{pr2}$

Tags: nameformwebhtmlbutton页面render按钮
1条回答
网友
1楼 · 发布于 2024-05-19 10:09:32

您必须讨论的是异步请求,在这种情况下,网页可以处理请求而不刷新页面。不像网页.py例如登录页面。在

别紧张。使用jQuery进行作业。documentation足够让你开始了。但在这里我给你举个例子。在

将此代码段添加到.js文件:

$.ajax({
   url: '/',
   type: 'POST',
   data: {
       params: text
}).done(function(){
    /* Do something */
});

相关问题 更多 >

    热门问题