UltraNurd
UltraNurd

Reputation: 1334

STATIC_URL undefined in base Django template

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.

Upvotes: 10

Views: 8361

Answers (4)

Nicolas Malbran
Nicolas Malbran

Reputation: 161

You need to add 'django.core.context_processors.static' to your TEMPLATE_CONTEXT_PROCESSORS variable in settings.py.

Upvotes: 15

Anurag Uniyal
Anurag Uniyal

Reputation: 88727

You can just add STATIC_URL to to template rendering by passing

{'STATIC_URL': settings.STATIC_URL} 

or you can add static context processor see doc

Upvotes: 0

dani herrera
dani herrera

Reputation: 51645

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.

Upvotes: 21

Brandon Taylor
Brandon Taylor

Reputation: 34553

You need to add 'django.core.context_processors.request' to your TEMPLATE_CONTEXT_PROCESSORS.

Upvotes: 0

Related Questions