I’ve been trying to follow every single tutorial and thread there is, i jast cant get it togheter.
Sorry, but please exlpain it to me as if i was three years old..
What i have:
a CSS that i’d like use, styling a template.
How to get there:
MD: project/app/static/
place style.css there
settings.py:
STATIC_ROOT = '/project/app/static/'
STATIC_URL = '/static/'
(Should’nt this at least give me the string ‘/static/’, when i try printing {{ STATIC_URL }} in the template..?)
template:
<LINK REL=StyleSheet HREF="{{ STATIC_URL }}/style.css" TYPE="text/css" MEDIA=screen>
Seems to me, the next logical thing would be to create an /static/-url aswell?
I’m really lost here..
Django’s staticfiles system is about two things:
Combining static files from a number of (app-specific) directories into a single place from which they can be served
Serving those files over HTTP
You want #1 if you have a number of static files located in different directories. You want #2 if you want to see those files while you are using the development server. In production, you usually skip #2, because the purpose of #1 is to put the files somewhere that your web server can access them.
Organizing and collecting static files
With that said, here’s how you get #1:
At this point, you can do a few things:
You put your static files in an ‘/static/’ directory under each of your apps. That’s where django will look for them. There is an additional setting,
STATIC_DIRS, that you can use if you have static files that are outside of your apps.You can run
manage.py collectstaticto have Django go through all of your apps, finding all of the static files, and copying them into/path/to/my/project/collected-static-files(That’s why that setting is a filesystem path.)You can use
{{ STATIC_URL }}in templates to access the files.Serving static files over HTTP
In development mode, under the dev server, Django will automatically serve files out of the
STATIC_ROOTdirectory.Basically, Django will add a URL handler for URLs of the form
And serve content from your hard drive, from the filesystem path
In production, this won’t happen automatically, so you will have to configure your web server to do the same thing. (It’s just not a good idea to run all of your static files through Django’s full processing pipeline. The web server can do it much more efficiently)