I just downloaded the latest version of Django (1.4.1), and I canβt figure out how to serve css files when developing locally using a server server. I read the corresponding Django docs on static files , and there are many, many questions and answers ... it looks like it should be more or less automatic, but it does not work for me.
I am working on a survey application from a textbook.
404 from the magazine
[27/Apr/2012 01:04:09] "GET /polls/ HTTP/1.1" 200 210 [27/Apr/2012 01:04:09] "GET /polls/css/styles.css HTTP/1.1" 404 2596
Directory structure
mysite |-- manage.py |-- mysite |-- __init__.py |-- settings.py |-- urls.py |-- wsgi.py |-- polls |-- __init__.py |-- models.py |-- tests.py |-- views.py |-- static |-- css |-- styles.css |-- templates |-- polls |-- index.html
index.html
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/styles.css">
settings.py
MEDIA_ROOT = '' MEDIA_URL = '' STATIC_ROOT = '' STATIC_URL = '/static/' STATICFILES_DIRS = () STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ) TEMPLATE_CONTEXT_PROCESSORS = ( "django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", 'django.core.context_processors.static', )
^^^ I did not have the TEMPLATE_CONTEXT_PROCESSORS variable in settings.py when I started the project and had to add it manually - should I worry about that?
STATICFILES_DIRS is empty because the css file is located in a directory called static in the polls application in which Django automatically searches for it - right?
I also have django.contrib.staticfiles in my INSTALLED_APPS.
urls.py
I saw in the docs that this solution works for local development servers other than runerver - it doesn't seem like it should be otherwise, right? (I currently commented on it.)
EDIT: I uncommented these lines, but didn't see the changes - I still get the same 404 in the css file
from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns += staticfiles_urlpatterns()
Is my directory structure incorrectly configured? Am I missing the necessary settings in settings.py? Any help would be much appreciated! Thanks!
EDIT:
I made a Mark suggestion and read in RequestContext. Change my view:
return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list})
to
from django.template import RequestContext ... return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list}, context_instance=RequestContext(request))
received / static / url for registration:
[27/Apr/2012 13:56:55] "GET /static/css/styles.css HTTP/1.1" 200 19
This fixes the problem.