1

こんにちは私は以下のhtmlコードを持っています

<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)からデータを取得できません<---私はNoneを取得します

4

1 に答える 1

3

変数を正しく渡していない。変数を渡す方法は2つあります。

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.
于 2012-07-19T15:49:44.547 に答える