Flask request request.form.get No
I am new to flask and am trying to learn. I am trying to access information from a query string. Here is my html code (simplestuff.html, it's 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 flag 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 start the server and navigate to 127.0.0.1:5000/simplestuff the html page works fine. Then when I select "Get Query String" the url changes as expected and the query string appears. However, when QueryStringInfo is loaded, None is returned instead of age. What am I doing wrong here?
Edit: changed request.form.get ("age") to request.args.get ("age"). The age of the seal was also changed to bring back the age. QueryStringInfo gives me 500 errors and doesn't return (probably due to 500 error).
source to share
What you are trying will not work the way you think. The final question is, "What are you trying to achieve?"
To access the query string on the server side, the query string must be included in the request. In other words, your age access will work if you refer to it:
http://127.0.0.1//QueryStringInfo?age=10
When accessing that route, your handler will correctly return 10. However, you are redirected to
http://127.0.0.1/simplestuff?age=10
Then you try to access /QueryStringInfo
without a query string and it just doesn't work.
So you have a couple of options.
-
Get
age
in a handler/simplestuff
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 }}
- Make an ajax request
"/QueryStringInfo?age=" + age
. But at this point I'm not sure why you should make an additional server request to access the query string variable when you already have access to it.
source to share
You are 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 default returns None
for functions that have no return value
EDIT
It is also age
not defined in this function. When you receive a query string 'age'
, you store it in a variable named length
. Is it age
global?
source to share