This question already has answers here:
Link to Flask static files with url_for
(2 answers)
Closed 5 years ago.
Here is my directory hierarchy
flaskApp
|_ flaskApp.py
|_static
| |_realsurfstyle.css
|
|_templates
|_Realsurfhtml.html
Here is the relevant render_template from the flaskApp.py file
#app.route('/<surfBreak>')
def waveHeight(surfBreak):
return render_template("Realsurfhtml.html", name=surfBreak)
Here is my code from the html file
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='static/realsurfstyle.css')}}"/>
I dont see what im missing?
You don't have to place static in the file path to the .css file. In essence you're telling it that path to the file is /static/static/realsurfstyle.css so instead try and write the path as href="{{ url_for('static', filename='realsurfstyle.css')}}"
Additionally, I would recommend placing the .css files in a folder in the static directory labeled css. which would change the file path to href="{{ url_for('static', filename='css/realsurfstyle.css')}}"
Related
This question already has answers here:
How to serve static files in Flask
(24 answers)
Closed 11 months ago.
Hello i have one code but don't know how to connect css in flask.
<link rel="stylesheet" href="css/styles.css">
But its not working. Is there any difference Syntex in flask?
make one folder as 'static' and make one more folder for css files so that you can put css file in that folder.
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
How do you use url_for in Flask to reference a file in a folder? For example, I have some static files in the static folder, some of which may be in subfolders such as static/bootstrap.
When I try to serve a file from static/bootstrap, I get an error.
<link rel=stylesheet type=text/css href="{{ url_for('static/bootstrap', filename='bootstrap.min.css') }}">
I can reference files that aren't in subfolders with this, which works.
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='bootstrap.min.css') }}">
What is the correct way to reference static files with url_for? How do I use url_for to generate urls to static files at any level?
You have by default the static endpoint for static files. Also Flask application has the following arguments:
static_url_path: can be used to specify a different path for the static files on the web. Defaults to the name of the static_folder folder.
static_folder: the folder with static files that should be served at static_url_path. Defaults to the 'static' folder in the root path of the application.
It means that the filename argument will take a relative path to your file in static_folder and convert it to a relative path combined with static_url_default:
url_for('static', filename='path/to/file')
will convert the file path from static_folder/path/to/file to the url path static_url_default/path/to/file.
So if you want to get files from the static/bootstrap folder you use this code:
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='bootstrap/bootstrap.min.css') }}">
Which will be converted to (using default settings):
<link rel="stylesheet" type="text/css" href="static/bootstrap/bootstrap.min.css">
Also look at url_for documentation.
In my case I had special instruction into nginx configuration file:
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
try_files $uri =404;
}
All clients have received '404' because nginx nothing known about Flask.
The primary configuration file is /etc/nginx/nginx.conf on Linux. It may be similar on Windows.
My CSS file attached to my python code isn't working, and I can't seem to figure out why since there are no errors and I've tried multiple ways of doing this.
Structure:
app{
|app
|templates
|public
|index.html
|env
|static
|css
|styles.css
I've attached it to index.html with
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
and in my python file I used
#app.route("/")
def index():
return render_template("public/index.html")
Everything works perfectly find but the CSS doesn't work on the page:
style.css
body {
color: red;
}
According to your project structure, the static sub-folder is outside the app folder. Consider adding it at the same location as templates.
project
| -- app
| -- templates/
| -- static/
Your <link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}"> should work.
Perhaps try to put the filename parameter to: `filename='/static/css/styles.css', and see if it works.
styles.css or style.css?
Check the css file name!
If that is a typo in your question and not in your main code, try reloading your page using Ctrl + Shift + R.
I am trying to serve a static image to a css file using flask. I am using render_template() to to get the html file, and I have a static directory for my css and javascript. My other image is loading, but the image that I am referencing from the css file is not working and I get his from the flask terminal: GET /static/static/res/typewriter.jpg HTTP/1.1" 404
I tried typing in the path in my browser and it works fine
This is how I am referencing the image since the CSS file is already in the static directory. background-image: url("/res/typewriter.jpg");
I hope there is an easy fix for this, especially since I am new to this stuff. Thanks!
In your html file you should have this in the header:
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='mainpage.css') }}">
... as long as your static directory is named static. In several of my Flask applications I am importing Bootstrap css like so:
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='bootstrap.css') }}">
Ensure you are visiting http://127.0.0.1:5000 in your browser (unless you changed the default).
Also here is a random example of how I serve images from the static directory:
h1 class="display-3"> <img src="/static/title_welcome.png" alt="about"></h1>
I'm trying to use this template: http://www.free-css.com/free-css-templates/page185/flat-style#shout
and this is my file directory:
app
- static
[--] css (inside static)
[--] images (inside static)
[--] js (inside static)
- templates
but the images won't load properly still:
http://i.imgur.com/Mh71ko9.png
Does anyone know why the images won't properly load?
You have simply dumped the HTML from the website in to your templates folder and all the href and src attributes for images and CSS are still using relative links. These need to be converted to use Flask's static files functionality.
For example, to get the CSS working, change this line:
<link href="css/bootstrap.css" rel='stylesheet' type='text/css' />
to this:
<link href="{{ url_for('static', filename='css/bootstrap.css') }}" rel='stylesheet' type='text/css' />
And similarly for images, you can change:
<img src="images/logo.png" title="Flat style" />
to
<img src="{{ url_for('static', filename='images/logo.png') }}" title="Flat style" />
You'll need to do this for all static files referenced in the HTML.
Note that your Flask app will need to have imported url_for
from flask import Flask, render_template, url_for