I can’t get the CSS to output correctly – my webpages are all unstyled.
This is my link in all my templates. What am I doing wrong?
<link type="text/css" rel="stylesheet" href="/stylesheets/style.css"/>
Is there anything special that I have to do with Flask to get it to work?
I’ve been trying and changing things for about half an hour but can’t seem to get it right.
To sum it up: How do you do CSS with Flask – do I have to have any special python code?
You shouldn’t need to do anything special with Flask to get CSS to work. Maybe you’re putting
style.cssinflask_project/stylesheets/? Unless properly configured, such directories won’t be served by your application. Check out the Static Files section of the Flask Quickstart for some more information on this. But, in summary, this is what you’d want to do:Move static files you need to
project_root/static/. Let’s assume that you movedstylesheets/style.cssintoproject_root/static/stylesheets/style.css.Change
to
This tells the template parser (Jinja2) to tell Flask to find the configured static directory in your project directory (by default,
static/) and return the path of the file./static/stylesheets/style.css. But you really shouldn’t do that – usingurl_forallows you to switch your static directory and still have things work, among other advantages.And, as @RachelSanders said in her answer: