Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask request.form.get returning None

I am new to flask and am trying to learn. I am trying to access information from the query string. Here is my html code (simplestuff.html, it is in the templates folder):

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
<select onchange="showUser(this.value)">
<option value="1">Get Something</option>
<option value="2">Get Query String</option>
</select>
</form>
</head>
<body>

<p id="demo"></p>
<ol id="new-projects"></ol>

<script>
function showUser(str) {
   if (str=="2") {
      //Age in the query string.
      var age = 30;

      //redirecting to a page with the query string in the url.
      location.href = "simplestuff.html?age=" + age;

      //Using flask to access age in the query string.
      $( "#new-projects" ).load( "QueryStringInfo" );
    }
}
</script> 
</body>
</html>

Here is my flask code (main.py) that tries to access age in the query string:

from flask import Flask, render_template, request

app = Flask(__name__, static_url_path='')

@app.route('/simplestuff')
def render_plot():
    return render_template('simplestuff.html')

@app.route('/QueryStringInfo')
def do_something():
    #Requesting the age variable in the query string.
    age = request.args.get('age')
    return age

if __name__ == '__main__':
    app.run()

When I run the server and go to 127.0.0.1:5000/simplestuff the html page runs fine. Then when I select "Get Query String" the url changes as it is supposed to do and the query string appears. However, when QueryStringInfo is loaded a "None" is returned instead of the age. What am I doing wrong here?

Edit: changed request.form.get('age') to request.args.get('age). Also changed print age to return age. QueryStringInfo is giving me a 500 error and no return (probably due to the 500 error).

like image 649
chewinggum Avatar asked Sep 27 '14 22:09

chewinggum


2 Answers

What you are trying will not work the way you think it should. The ultimate question is "What are you trying to achieve?"

In order to have access to the query string on the server side, the query string needs to be included in the request. That is to say, your access of age would work if you accessed:

http://127.0.0.1//QueryStringInfo?age=10

When you access that route, your handler will appropriately return 10. However, you are redirecting to

http://127.0.0.1/simplestuff?age=10

You then try to access /QueryStringInfo without the query string, and it simply doesn't work that way.

So you have a couple options.

  1. Retrieve age in the /simplestuff handler and pass it to the template as a context variable.

    @app.route('/simplestuff')
    def render_plot():
        age = request.args.get('age')  
        return render_template('simplestuff.html', age=age)
    

And in your template:

   {{ age }}
  1. Make the ajax request to "/QueryStringInfo?age=" + age. But at that point I am not sure why you would make an additional server request to access the query string variable when you already have access to it.
like image 188
sberry Avatar answered Nov 17 '22 17:11

sberry


You're printing age instead of returning it

@app.route('/QueryStringInfo')
def do_something():
    #Requesting the age variable in the query string.
    length = request.form.get('age')
    print age # should be return age

Python defaults to returning None for functions that have no return value

EDIT

In addition, age is not defined in that function. When you get the 'age' query string, you're storing it in a variable called length. Is age a global?

like image 1
Peter Gibson Avatar answered Nov 17 '22 19:11

Peter Gibson