-1

I'm trying to server an HTML page with some Javascript from a Flask Server

Python Code:

from flask import Flask
from flask import render_template
app = Flask(__name__)

@app.route("/")
def hello():
    return render_template('simple.html')

if __name__ == "__main__":
    app.run()

My directory looks something like this:

enter image description here

The HTML file I'm rendering is in the templates file.

To load the JS file I have <script type="text/javascript" src="../static/js/script.js"></script>

I keep getting an error

"browser.min.js:3 GET http://localhost:5000/script.js 404 (NOT FOUND)n.load @ browser.min.js:3s @ browser.min.js:3i @ browser.min.js:3
browser.min.js:3 Uncaught Error: Could not load http://localhost:5000/script.js(…)s.onreadystatechange @ browser.min.js:3

What am I doing wrong?

arhak
  • 2,488
  • 1
  • 24
  • 38
Morgan Allen
  • 3,291
  • 8
  • 62
  • 86

2 Answers2

0

You need to write paths relative to URL loaded by the browser, not the physical path on disk where your template lives.

The browser has no idea that the HTML it serves was written in a file in the templates directory; it just sees HTML source behind a URL.

It's usually best to always use absolute paths, especially if your template can be served by views from multiple routes.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

try without the dots: <script type="text/javascript" src="//static/js/script.js"></script>

http://webpy.org/cookbook/staticfiles

or let the proper path be built from within python url_for('static', filename='js/script.js')

http://flask.pocoo.org/docs/0.11/quickstart/#static-files

arhak
  • 2,488
  • 1
  • 24
  • 38