Jinja variable for flask-app root directory

I am developing an application using the very convenient Flask system, and I used the jinja template to write a lot of links that correspond to pages in Flask, which are defined as follows:

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

on the test server, these links work very well, however, when I switch from the test server to the server behind the proxy server, I get a problem when the href links do not 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 wonder if, instead of putting a backslash, there is a way to put a variable that indicates the 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 to the Flask team!

+6
source share
2 answers

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

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

Second, if you are behind a reverse proxy, wrap the WSGI application with this decorator , which updates the request environment so that the check box generates the correct URL.

+6
source

Perhaps you are looking for a url_for function?

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

Source: https://habr.com/ru/post/909142/


All Articles