2012-07-19 5 views
1

안녕 난이플라스크 양식 및 자바 스크립트

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

아래의 HTML 코드 자바 스크립트 코드

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); 
     }  
} 

와 플라스크에 파이썬 코드 내가 뭘 잘못

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 이벤트 내에서 정의되지 않았습니까? –

+0

아니요. 자바 스크립트 부분이 잘 작동합니다. 또한 http : // localhost : 5000/tx/checkme/filepathz = "USERS INPUT"이라는 새 링크를 열지 만 파이썬에 전달하지는 않습니다! –

답변

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을 통해 전달하는 경우 요청 변수는 url 구조의 일부가 아니므로보기 장식자는 @ app.route ('/ tx/checkme /') 여야합니다. –

+0

내 생명을 구해준 많은 고마워! –