1

hello i have the html code below

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

the javascript code

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

and the python code on flask

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)

what am i doing wrong and can't get the data from the form (filepathz) <--- i get None

2
  • 1
    can you verify that your events trigger? (with console.log)? and that filepathz variable isn't undefined inside the onclick event? Commented Jul 19, 2012 at 15:14
  • no it's working fine javascript part. It also goes and opens the new link localhost:5000/tx/checkme/filepathz=" USERS INPUT" but it doesn't pass that to python! Commented Jul 19, 2012 at 15:17

1 Answer 1

3

You're not passing the variable correctly. There are two ways to pass the variable:

1) Pass it via get method:

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

Currently you are trying to get the variable from request.args but not passing it in the request, that's why you get none.

2) Get it from the url with flask's url structure:

Do this in JS : http://localhost:5000/tx/checkme/" + path

And in you view :

@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.
Sign up to request clarification or add additional context in comments.

1 Comment

And if you pass it via GET, your view decorator should be : @app.route('/tx/checkme/') , as request variables are not a part of the url structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.