I have a template, base.html
, which is used in several other templates for various views. Each of those templates starts with the appropriate {% extends "base.html" %}
. In the base template, I want to specify a static stylesheet thusly:
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/base.css"/>
However, when it renders most of my templates, the value of STATIC_URL
is empty, so the attribute is merely href="/base.css"
, which doesn't load. The variable is properly defined for the template that I have tied to the default login view, django.contrib.auth.views.login
, but for my own custom views, it's undefined.
I am just trying to get this working in the development environment with runserver
, so the CSS file is in the static subdirectory of the app.
Here are the relevant bits from my settings.py
, which are all the defaults:
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
In my urls.py
I also have added:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
#...
urlpatterns += staticfiles_urlpatterns()
Any ideas what I'm doing wrong? As far as I can tell this is what you're supposed to do for serving app-specific static files in development, based on the 1.3 documentation.
Perhaps this can help:
If {{ STATIC_URL }} isn't working in your template, you're probably not using RequestContext when rendering the template.As a brief refresher, context processors add variables into the contexts of every template. However, context processors require that you use RequestContext when rendering templates. This happens automatically if you're using a generic view, but in views written by hand you'll need to explicitly use RequestContext To see how that works, and to read more details, check out Subclassing Context: RequestContext.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With