Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a jinja variable for flask-app root directory

I have been developing an app using the very easy-to-pickup Flask system and I have used a jinja template to write out a bunch of links that correspond to pages in Flask that are defined by something like this:

@app.route(/<var1>/<var2>)
...
...

in the test server these links work just fine however when I move from the test server to a server behind a proxy I get a problem where the href links don't take into account the extra directory name inserted by my proxy.

#where a link should read:
server:/myapp/<var1>/<var2>
# it acually puts out:
server:/<var1>/<var2>

my jinja etemplate looks like this but I am wondering if instead of putting a backslash there is a way to put a variable that specifies root directory.

{% block navigation %}
{% for record in db.values() %}
 <li><a href="/{{db.name}}/{{record.name}}">{{record.name}}</a></li>
{% endfor %}
{% endblock %}

Any help would be greatly appreciated. Thanks Flask team!

like image 892
zach Avatar asked Feb 23 '12 01:02

zach


1 Answers

Firstly, as @reclosedev said, you can generate URLs using the url_for function (assuming the view function is called myview:

<a href="{{ url_for('myview', var1=db.name, var2=record.name) }}">

Secondly, if you're behind a reverse proxy, wrap the WSGI application with this decorator that updates the request environment so that Flask generates correct URLs.

like image 189
Alex Morega Avatar answered Sep 18 '22 12:09

Alex Morega