2012-07-19 34 views
1

你好,我有下面的HTML代码瓶的形式和javascript

<form> 
<input type="text" id="filepathz" size="40" placeholder="Spot your project files"> 
<input type="button" id="spotButton" value="Spot"> 
</form> 

的JavaScript代码

window.onload = init; 

    function init() { 
      var button = document.getElementById("spotButton"); 
      button.onclick = handleButtonClick; 
    }  

    function handleButtonClick(e) { 
      var filepathz = document.getElementById("filepathz"); 

     var path = filepathz.value; 

     if (path == "") { 
       alert("give a filepath"); 
     }else{ 
       var url = "http://localhost:5000/tx/checkme/filepathz=" + path; 
       window.open (url,'_self',false); 
     }  
} 

,并在瓶中的Python代码

def index(): 
     """Load start page where you select your project folder 
     or load history projects from local db""" 
     from txclib import get_version 
     txc_version = get_version() 
     prj = project.Project(path_to_tx) 

     # Let's create a resource list from our config file 
     res_list = [] 
     prev_proj = '' 
     for idx, res in enumerate(prj.get_resource_list()): 
       hostname = prj.get_resource_host(res) 
     username, password = prj.getset_host_credentials(hostname) 
     return render_template('init.html', txc_version=txc_version, username=username) 

    @app.route('/tx/checkme/<filepathz>') 
    def checkme(filepathz): 
      filepathz = request.args.get('filepathz') 
      return render_template('init.html', txc_version=filepathz) 

我在做什么错了,无法从表单获取数据(filepathz)< ---我得到无

+1

你可以验证你的事件触发? (与console.log)?并且那个filepathz变量在onclick事件中没有定义? – 2012-07-19 15:14:40

+0

不,它的工作很好的JavaScript部分。它也会打开新的链接http:// localhost:5000/tx/checkme/filepathz =“USERS INPUT”,但它不会将它传递给python! – 2012-07-19 15:17:16

回答

3

您没有正确传递变量。有两种方法来传递变量:

1)通过GET方法传递下去:

http://localhost:5000/tx/checkme/?filepathz=" + path; (Note the '?') 

您正在试图从request.args中的变量,但不是通过它的要求,这就是为什么你没有得到。

2)从URL与瓶中的URL结构得到它:

为此在JS:http://localhost:5000/tx/checkme/" + path

而且在您查看:

@app.route('/tx/checkme/<filepathz>') 
def checkme(filepathz): 
     return render_template('init.html', txc_version=filepathz) # You can use this variable directly since you got it as a function arguement. 
+1

如果你通过GET传递它,你的视图装饰应该是:@ app.route('/ tx/checkme /'),因为请求变量不是url结构的一部分。 – 2012-07-19 16:09:52

+0

非常感谢,拯救了我的人生! – 2012-07-25 11:23:13