So I'm going to go out and get it out of the way that I know this is probably a terrible way of doing serving static files for my website. I've come across a lot of other threads (that haven't quite solved my problem) that alluded to the fact that I should use a webserver like apache to serve static files. I've been doing that thus far for my static website but I mostly just wanted to mess around with it this way for development purposes while I learn Flask and add its functionality to the website.
So basically what's been causing me trouble is I want to serve all my static files(JS, CSS, etc.) using Flask and also route all my static webpages using flask as well (just for the time being, so I can run my app without apache and still load all of the links). My app setup is I have an 'app.py' with all my code and a 'static' directory for all my static files and a 'templates' folder for all my webpages
This is my code so far:
from flask import Flask, render_template
# Create the application object
app = Flask(__name__, static_url_path='/static')
# Routes to path
@app.route('/')
def home():
return render_template('index.html')
# Routes to specific file, want to route all my pages instead
@app.route('/work.html')
def work():
return render_template('work.html')
#Route methods for other webpages
# Serves static files
@app.route('/<path:path>')
def static_file(path):
return app.send_static_file(path)
# Start the server
if __name__ == '__main__':
app.run(debug=True)
Now, there's a few notable problems with this code but unfortunately, this is best I could get it to run properly. For one, I've been reading a lot that 'send_from_directory' instead of 'send_static_file' is the proper way to send static files in Flask but I couldn't quite get that to work. But my main concern for the time being is that I can't route all my static webpages this way (and I would have thought that 'static_file' would have done this, in addition to serving all my JS, CSS, etc.); as it stands I only can route my webpages by creating a seperate function in 'app.py' for every webpage which seems terribly inefficent. So yeah, if there's a few lines in my code I need to change, I would be very grateful for being pointed in right direction!