I was having issues with a js script not running in my Flask app and found that the root path was nothing. After that, i setup a simple test page like this:
@app.route('/test')
def index2():
return render_template('test.html')
test.html:
{{ request.script_root|tojson|safe }}
And it just prints:
""
What is wrong?
Ins't the script_root
empty because you are directly on the root of that server?
From Flask docs:
Do you know where your application is? If you are developing the answer is quite simple: it’s on localhost port something and directly on the root of that server.
@app.route('/test')
def index2():
if not request.script_root:
# this assumes that the 'index' view function handles the path '/'
request.script_root = url_for('index', _external=True)
return render_template('test.html')
test.html:
{{ request.script_root|tojson|safe }}
Renders:
"http://localhost:5000/"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With