2014-02-10 51 views
0

我有一個頁面有3個按鈕。當我按下時,我想讓頁面告訴我我按下了哪個按鈕。最後,我想切換進程並從該頁面運行腳本,並立即獲得按鈕旁邊的反饋。就像一個控制面板。與web.py的表單按鈕例程

我的問題是採取按鈕操作,做一些事情,並返回結果到頁面的按鈕。

我讀過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> 

回答

1

你說的肯定是異步請求,其中網頁可以處理請求而無需刷新頁面。不像web.py的例子,如登錄頁面。

簡單。使用jQuery作業。 documentation綽綽有餘,讓你開始。但在這裏我會給你一個例子。

這個代碼段添加到您的.js文件:

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