Reputation: 73
I just downloaded the latest Django version (1.4.1), and I can't figure out how to serve css files when developing locally using runserver. I've read the relevant Django docs on static files and many, many questions & answers here... sounds like it's supposed to be more or less automatic, but it's not working for me.
I'm working on the polls app from the tutorial.
404 from the log
[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 didn't have the TEMPLATE_CONTEXT_PROCESSORS variable in settings.py when I started the project and had to add it manually - should I be worried about that?
STATICFILES_DIRS is empty, because the css file is in a directory named static within the polls app, which is where Django looks for it automatically - 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 runserver - sounds like it shouldn't be necessary otherwise, right? (I currently have it commented out.)
EDIT: I uncommented these lines, but did not see a change - still getting the same 404 on the css file
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
Do I have my directory structure set up wrong? Am I missing necessary settings in settings.py? Any help would be very much appreciated! Thanks!
EDIT:
I took Mark's suggestion and read up on RequestContext. Changing my view from:
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))
got the /static/ url to register:
[27/Apr/2012 13:56:55] "GET /static/css/styles.css HTTP/1.1" 200 19
This fixes the problem.
Upvotes: 7
Views: 8172
Reputation: 2203
This worked for me under development server:
1. I added "static" folder under my application "myblog" directory.
2. Added "myblog.css" file inside "static" folder.
3. Linked the style sheet to my application's template, code is below.
<link rel="stylesheet" href="{{ STATIC_URL }}myblog.css" type="text/css" media="screen" />
I did not change settings.py or urls.py files in order to fix css, the changes were made are only according to tutorial, so you should have the same.
Upvotes: 0
Reputation: 565
.' I had the same problem, and evnetually with the same coinfuration as mentioned above this did the trick: i added in setting.py the path to my static directory in STATICFILES_DIRS:
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR,'static'),
)
when also in settings.py PROJECT_DIR is set to:
PROJECT_DIR=os.path.dirname(os.path.abspath(__file__))
Upvotes: 1
Reputation: 25164
In order to use STATIC_URL
in the template you need to be sure you are using a RequestContext
along with adding 'django.core.context_processors.static'
to TEMPLATE_CONTEXT_PROCESSORS
. This is done for you if you are using the render
shortcut. If you are not using a RequestContext
then you can use the {% get_static_prefix %}
template tag from the staticfiles
template tag library. This is detailed in the docs here: https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#other-helpers
Upvotes: 10
Reputation: 25489
From your 404, looks like you're not including {{ STATIC_URL }} in your template. That's probably the problem. instead of just "css/file", do "{{ STATIC_URL}}css/file"
Without looking too hard, your settings looks fine. If the above doesn't work, try running ./manage.py collectstatic and see if that helps (it moves static files from app directories into a project static directory).
And definitely uncomment those lines in your urls.py! If you don't have static url patterns, when it goes to request something from /static/, it won't know what to do.
Upvotes: 0