简体   繁体   中英

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 .

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.

您需要将“django.core.context_processors.static”添加到settings.py中的TEMPLATE_CONTEXT_PROCESSORS变量中。

您需要将'django.core.context_processors.request'添加到TEMPLATE_CONTEXT_PROCESSORS。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM