0

So I'm migrating from apache to using flask, I've moved all my css, javascript, csv and Images to static. From there I've spent a while changing all the hrefs so everything loads correctly.
Everything the html is loading is working fine, but all of the requests javascript is sending are 404'ing and I'm not sure why.

My directory structure is as follows:
Python:
-------static:
---------------css:
---------------csv:
------------------------gamesToday.csv
---------------js:
---------------Images:
------------------------logo.png
------------------------logo-small.png
-------templates:
------------------main.html
-------flaskTest.py
My flasktest.py simply loads and renders the main.html file at the moment.

Javascript request for csv file:

$.ajax({
    type: "GET",
    url: "{{url_for('static', filename='csv/gamesToday.csv')}}",
    dataType: "text",
    success: function(data) {processData(data);}
 });

Javascript for loading image:

function logoSizing(){
    if($(window).width() < 992)
        $("#logo").attr("src", "{{url_for('static', filename='Images/small-logo.png");
    else
        $("#logo").attr("src", "{{url_for('static', filename='Images/logo.png");
}

The strange thing is when i move the request for the image into html the image loads correctly, so I know that the file is infact there, so I presume the error is in the fact I can't make an AJAX call like this? If so, how do I go about making it correctly?

Apologies if this is very simple, just started learning flask today and I'm having serious trouble wrapping my head around it.

2
  • .js files are not parsed by the jinja template engine so it wont replace {{url for... you need to pass the url in from one of your html templates or hardcode the path Commented Sep 21, 2018 at 16:58
  • @JoranBeasley Knew I was doing something stupid. If you fancy making it an answer I'll be more than happy to choose it as a solution! Commented Sep 21, 2018 at 17:01

2 Answers 2

0

.js files are not parsed by the jinja template engine so it wont replace {{url for...}} with the actual url.

you will need to pass the url in from one of your html templates or hardcode the path

Sign up to request clarification or add additional context in comments.

Comments

0

for js files:

{{url_for('static',filename='main.js')}}

for images files:

{{url_for('static',filename='main.js')}}

example:(adding script in html file)

<script src="{{url_for('static',filename='main.js')}}"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.