Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

flask

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?

like image 761
Gabriel Avatar asked Feb 06 '16 20:02

Gabriel


1 Answers

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/"
like image 134
Dušan Maďar Avatar answered Oct 17 '22 02:10

Dušan Maďar