Trying to get external CSS files to work on the Django 1.3 development server has been a challenge. Despite reading through Django's 'managing static files' guide and various similar Stack Overflow questions, I still can't seem to figure out what I'm doing wrong.
The CSS styles don't display correctly when I navigate to localhost:8000/page
. What could be causing this issue?
Directory Layout
myproject
|-- manage.py
|-- settings.py
|-- urls.py
|-- app
|-- __init__.py
|-- models.py
|-- tests.py
|-- views.py
|-- static
|-- css
|-- page.css
|-- templates
|-- app
|-- page.html
myproject/views.py
import django.http
import django.template.loader
import django.template
def view_page(request):
template = django.template.loader.get_template("page.html")
context = django.template.Context()
return django.http.HttpResponse(template.render(context))
myproject/urls.py
from django.conf.urls.defaults import patterns, include, url
import myproject.app.views
urlpatterns = patterns('',(r'page/$', myproject.app.views.view_page),)
myproject/templates/app/page.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}page.css" />
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
myproject/settings.py
(excerpt)
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
"/home/myusername/Desktop/myproject/app/static",
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
ROOT_URLCONF = 'myproject.urls'
TEMPLATE_DIRS = (
'/home/myusername/Desktop/myproject/templates/app',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'app'
)