0

I have a problem. Flask application respond a 404 not found at this .css url.

enter image description here

How can I write this jinja code in javascript file ?

{{ url_for('static', filename='assets/css/themes/') }}

Javascript file

var setColor = function (color) {
            var color_ = (App.isRTL() ? color + '-rtl' : color);
            $('#style_color').attr("href", "assets/css/themes/" + color_ + ".css");
        }
deHaar
  • 17,687
  • 10
  • 38
  • 51
spehlivan
  • 223
  • 3
  • 6

1 Answers1

0

Jinja 2 is expected to template html files only : unless you put your script in the html and template it, which I would discourage, you cannot use templating directly in your javascript static file.

You can however template a part of the html that will contain the link, for example in a data attribute, or in a shared variable, to be recovered by javascript.

In the page

<div id="mydiv" data-myurl='{{ url_for('static', filename='assets/css/themes/') }}'> </div>

In javascript

 $('#style_color').attr("href", $("#mydiv").data('myurl'))
Diane M
  • 1,503
  • 1
  • 12
  • 23