Home Ask Login Register

Developers Planet

Your answer is one click away!

Gabriel A. Zorrilla February 2016

Flask {{ request.script_root|tojson|safe }} returns nothing

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?

Answers


dm295 February 2016

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/"

Post Status

Asked in February 2016
Viewed 1,225 times
Voted 14
Answered 1 times

Search




Leave an answer


Quote of the day: live life