Rookie
Rookie

Reputation: 1600

js scripts not loading in jinja2 template

I have a situation where, replacing a url string "/CreditHistory/10216" with a jinja2 variable {{creditNumbers|safe}}, messes up the loading of javascript files. More specifically, this works;

{% block Scripter %}

<script type="text/javascript" src="./static/assets/js/crossfilter/crossfilter.js"></script>
<script type="text/javascript" src="./static/assets/js/d3/d3.js" charset="utf-8"></script>
<script type="text/javascript" src="./static/assets/js/dc.js/dc.js"></script>
<script type="text/javascript" src="./static/assets/js/queue/queue.js"></script>
<script src='./static/assets/js/graphsFordringer.js' type='text/javascript' charset="utf-8"></script>
<script type="text/javascript"> 

queue().defer(d3.json, "/CreditHistory/10216").await(makeGraphs);

</script>

{% endblock %} 

But, this does not;

{% block Scripter %}

<script type="text/javascript" src="./static/assets/js/crossfilter/crossfilter.js"></script>
<script type="text/javascript" src="./static/assets/js/d3/d3.js" charset="utf-8"></script>
<script type="text/javascript" src="./static/assets/js/dc.js/dc.js"></script>
<script type="text/javascript" src="./static/assets/js/queue/queue.js"></script>
<script src='./static/assets/js/graphsFordringer.js' type='text/javascript' charset="utf-8"></script>
<script type="text/javascript"> 

queue().defer(d3.json, "{{creditNumbers|safe}}").await(makeGraphs);

</script>

{% endblock %} 

The errors that gets thrown in the web-browser implies that none of the javascript files get loaded. One of them is for example that queue is not a defined function. What is also apparent is that the "{{creditNumbers|safe}}" variable does load to "/CreditHistory/10216". So, in short the variable loading seems to break the javascript loading. Not that I have found reference to similar issues in the documentation, so that probably is not what is happening.

EDIT:

It now seems that I have misunderstood the entire situation. It looks like it is the way that the jinja2 template variable is declared in the app.py file that is the root cause.

The @app.routecode that is failing is;

@app.route('/KundeFordringer/<int:KundeNr>')
def fordringer(KundeNr):

    jsonSti = "/CreditHistory/"+str(KundeNr)
    return render_template("fordringer.html", creditNumbers=jsonSti)

However, if I change the code to the following, it works fine;

@app.route('/KundeFordringer')
def fordringer():

    return render_template("fordringer.html", creditNumbers="/CreditHistory/10216")

As mentioned previously, viewing the source code from the web browser, one could see that the "/CreditHistory/10216"was loaded when using the first @app.route declaration. But apparently something is, never the less, off with that way of doing it.

Any help would be greatly appreciated

Upvotes: 1

Views: 2427

Answers (1)

Timoth&#233;e Jeannin
Timoth&#233;e Jeannin

Reputation: 9912

The loading of the javascript files is relative to the current url. Basically if you are browsing

http://mywebsite.com/KundeFordringer/456

Then the browser is going to try to load those files:

http://mywebsite.com/KundeFordringer/456/static/assets/js/crossfilter/crossfilter.js
http://mywebsite.com/KundeFordringer/456/static/assets/js/d3/d3.js
http://mywebsite.com/KundeFordringer/456/static/assets/js/dc.js/dc.js
http://mywebsite.com/KundeFordringer/456/static/assets/js/queue/queue.js

What you want is probably

http://mywebsite.com/static/assets/js/crossfilter/crossfilter.js
http://mywebsite.com/static/assets/js/d3/d3.js
http://mywebsite.com/static/assets/js/dc.js/dc.js
http://mywebsite.com/static/assets/js/queue/queue.js

Their might be a problem either with your script tags:

<script type="text/javascript" src="./static/assets/js/dc.js/dc.js"></script>

That needs to be renamed

<script type="text/javascript" src="/static/assets/js/dc.js/dc.js"></script>

Or a problem with your static_url_path.

Upvotes: 1

Related Questions