Reputation: 4733
So I want to include some js and css files in my html page that flask serves but want them in /js/ and /cs/ instead of all in /static/ grouped together.
I tried to do this More than one static path in local Flask instance
using:
(part of the randomly named cat.html)
<link rel="stylesheet" href="{{url_for('css_static',filename='bootstrap.min.css')}}">
<link rel="stylesheet" href="/static/css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="/css/prettify.css">
and
(part of app.py)
@app.route('/sigma')
def sig():
return render_template('cat.html')
@app.route('/css/<path:fn>')
def css_static(fn):
return send_from_directory('/css/',fn)
@app.route('/js/<path:fn>')
def js_static(fn):
return send_from_directory(app.config['JS_STATIC'],fn)
when I try this it doesn't work (the /cat.html
) shows Internal Server Error.
and when I go to /css/bootstrap-responsive.min.css
it loads forever....
so what am I doing wrong?
Edit: it seems the only directory open is /static/...
which I don't know why since I have a /js/
route.
Upvotes: 1
Views: 1207
Reputation: 11
you can do like this:
from flask import Flask, render_template, send_from_directory
app = Flask(__name__)
@app.route("/")
def Home_page():
return render_template("index.html")
@app.route('/images/<filename>')
def images_folder(filename):
return send_from_directory('images', filename)
@app.route('/css/<filename>')
def css_folder(filename):
return send_from_directory('css', filename)
@app.route('/js/<filename>')
def js_folder(filename):
return send_from_directory('js', filename)
if __name__ == "__main__":
app.run(debug=True)
If you want to download all files: click here
Upvotes: 1
Reputation: 2116
I don't think you need to define a route for that. Just place your "js" and "css" directories inside the static dir.
Here's what i do :
<link rel="stylesheet" href="{{ config["APP_URL"] }}/static/css/style.css">
replace the app url part with your domain.
Upvotes: 1