I am having problem with my static CSS not working for my Django web app. I have followed the directions from the Django Static Link tutorial on handling static files, but it is still not working.
Settings
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/Users/a9austin/Development/sites/AlphaSocks/src/static_root/'
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'/Users/a9austin/Development/sites/AlphaSocks/src/staticfiles'
)
view
#from django.http import HttpResponse
from django.shortcuts import render_to_response
def index(request):
return render_to_response('index.html')
index.html
<link rel="stylesheet" href="{{STATIC_URL}}css/style.css" type="text/css" media="screen" >
And my directory organization is
src->staticfiles->css->style.css
For Django to serve static files, you have to make sure you have a couple of settings.
STATIC_URL
This setting specifies what URL should static files map to under. You have that done already.
STATICFILES_DIRS
This specifies all the folders on your system where Django should look for static files. The idea is that you might have a couple of apps within your project, and each app might require a different set of static files. So for organizational purposes, each app might contain a
staticdirectory where it will store only its static files. So then Django has to have a way to know where those directories are. This is what this setting is for.STATIC_ROOT
This setting specifies where Django will copy all the static files to and not where the static files are already at. The idea is that once you leave development into production, Django can’t serve static files anymore due to issues I will not go here (it’s in the article). However, for production, all static files should be in a single directory, instead of in many like specified in
STATICFILES_DIRS. So this setting specifies a directory to which Django will copy all the static files from all files withinSTATICFILES_DIRSby running the following command:Please note this is only necessary once you go into production and also that the directory specified here cannot be the same as any directory specified in
STATICFILES_DIRS.Urls.py
In development for Django to serve your static files, you have to include the static URLs in your urls.py:
Once you will complete all of the above things, your static files should be served as long as you have
DEBUG = True. Out of the list above, you seem to only completeSTATIC_URL. Also please note that all the steps I described above are in the docs you linked in your question (link). It might be a bit confusing in the beginning but if you read it a couple of times, it becomes clearer.