0

I have a basic flask folder hierarchy. I have a templets folder which holds all my HTML files, and a static folder for my javascript files. I have a javascript that builds a table. I have run this script from my html file directly by placing the script tag in the HTML file, and it works. I want to move it out of the HTML file and place it in the static folder. When I do that and try to source the file I get the error that the script is not found.

I have tried two different ways to do this and the file is still not getting found. What am i doing wrong here?

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

here is the exact error I get for both of these

Failed to load resource: the server responded with a status of 404 (NOT FOUND)
Max Powers
  • 1,119
  • 4
  • 23
  • 54
  • Possible duplicate of [How to serve static files in Flask](https://stackoverflow.com/questions/20646822/how-to-serve-static-files-in-flask) – janos Jun 04 '17 at 19:42

1 Answers1

0

If you haven't already, you need to set up routes to send the files that the webpage requests from your flask server, you can use send_from_directory to let requests get any files from a specified directory, in your case, the folder containing your javascript.

@app.route('/js/<path:path>')
def send_js(path): return send_from_directory('js', path)
JordanOcokoljic
  • 357
  • 2
  • 14
  • Not exactly sure why but this is working for me I changed my browers from Chrome to Firefox. There were some strange things going on when I used my Chrome browser. – Max Powers Jun 04 '17 at 23:28
  • Try clearing your cache in Chrome, as well as seeing if the error is reproducible in other non-webkit browsers such as Edge. I don't see why only Chrome would error. – JordanOcokoljic Jun 04 '17 at 23:29