Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 321
UltraNurd Avatar asked Dec 02 '11 19:12

UltraNurd


1 Answers

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.

like image 174
dani herrera Avatar answered Oct 20 '22 07:10

dani herrera