简体   繁体   中英

flask javascript not found in html file

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM