Reputation: 27
Learning how to add style sheet to django site. Have added the code I think I need, but the site completely ignores it. Here are my files;
urls.py
import os.path
from django.conf.urls.defaults import *
from users.views import *
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
stylesheets = os.path.join(os.path.dirname(__file__), 'stylesheets')
urlpatterns = patterns('',
(r'^$', main_page),
(r'^stylesheets/(?P<path>.*)$',
'django.views.static.serve',
{ 'document_root': stylesheets }),
# Examples:
# url(r'^$', 'chapter6.views.home', name='home'),
# url(r'^chapter6/', include('chapter6.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)
The main_page.html file
<html>
<head>
<title>The users application</title>
<link rel="stylesheet"
href="/var/chapter6/stylesheets/style.css"
type="text/css" />
</head>
<body>
<h1>The users application</h1>
Here are the users:
<p>{{ username1 }} </p>
<p>{{ username2 }} </p>
<p>{{ username3 }} </p>
</body>
</html>
Lastly, here is the style sheet.
body {
font-size: 12pt;
background-color: pink;
}
h1 {
color: red;
}
p {
font-size: 20pt;
}
I have gone over all the code a dozen time and can't find anything wrong. I am learning django from the book "django: visual quick pro guide". All the code looks right by the book. I have found a few errors in the book though. Any help would be greatly appreciated.
Thanks, Bobby
Upvotes: 0
Views: 1554
Reputation: 239350
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:
Upvotes: 0
Reputation: 599638
Your URLconf maps '/stylesheets/' but your HTML is looking at '/var/chapter6/stylesheets'.
Upvotes: 1