Juan Prado Juan Prado - 1 year ago 169
HTML Question

How to insert a JavaScript variable into Flask url_for() function?

I have following code in my Flask template:

<div id="cell1"></div>
var id="0014";
cell1.innerHTML = '<a href={{url_for('static',filename='+id+'".txt")}}">'+id+'</a>';

I want the link to render to:


But I got this:


How to make the js variable
in Flask
function work?

Thanks for your help!

Answer Source

Try this:

cell1.innerHTML = '<a href={{url_for('static', filename='')}}' + id + '.txt>' + id + '</a>';

url_for() will generate a link like this: .../static/<filename>. If you use url_for('static', filename=''), it generate a link like: .../static/, so you can just add text after it (i.e. + id + '.txt>') .

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download