user2260199 user2260199 - 9 months ago 42
Javascript Question

Flask Application - How to link a javascript file to website

I am having some trouble getting started with using javascript files on my website (A Flask application). I start the website by running which looks like this:

from app import app

In my html page I have the following code which gives a 404 error:

404 error

Error message:

error message

My file structure looks like this:

file structure

Any hints to where I'm going wrong?

Answer Source

Ah yes, luckily I am currently developing a flask application at the moment.

You are currently missing the static folder which by default flask looks into, a folder structure something like this:

        - html files are here
        - css and javascript files are here

Two important default folders that Flask will look into templates and static. Once you got that sorted you use this to link up with your javascript files from your html page:

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

Hope that helps any questions just ask.

Plus - A good article to read but not super related but it talks about the folder structure of flask is this: