zach
zach

Reputation: 30983

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!

Upvotes: 6

Views: 3292

Answers (2)

Alex Morega
Alex Morega

Reputation: 4228

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.

Upvotes: 6

reclosedev
reclosedev

Reputation: 9502

Maybe you are looking for url_for function?

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

Upvotes: 3

Related Questions