1👍
Your URLconf maps ‘/stylesheets/’ but your HTML is looking at ‘/var/chapter6/stylesheets’.
0👍
That book was published in 2009 (and doesn’t have very good reviews, anyways). A lot has changed in Django since 2009, so I would look for another starting place, if I were you. Unfortunately, though, there’s no many in-print Django books that are much more up-to-date.
The best place to start, though, id the online Django book. It’s the same as the in-print Definitive Guide to Django but is constantly updated online as Django itself changes.
That said, I’m assuming if you’re just starting that you’re using the latest version of Django (1.4 at the time). If not, upgrade immediately. In Django 1.3+, handling static files is trivial, and requires just the following settings:
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media')
MEDIA_URL = '/media/'
Add django.contrib.staticfiles
to INSTALLED_APPS
and you may need to add the following lines to your urls.py
:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
In development, Django will then serve anything in each app’s static
directory under STATIC_URL
. If you need project-wide resources, put them in another directory, such as “assets”, and then add that directory to STATICFILES_DIRS
:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'assets'),
)
When you go to production, STATIC_ROOT
and MEDIA_ROOT
will be served by your webserver instead of Django. For the purposes of static files, you’ll need to run:
python manage.py collectstatic
To have Django copy everything into STATIC_ROOT
. This is only for production. This directory should not even exist in development.
See also: