65👍
There is a static context-processor (Version 1.8), which isn’t the same as the media
one. You need to make sure you have django.core.context_processors.static
in your context-processor list, so it can add STATIC_URL
to the context.
As commented, for Django 3.0, that is now at django.core.context_processors.static. Django sure has changed a lot since 2011…
18👍
From the docs:
If {{ STATIC_URL }} isn’t working in your template, you’re probably not using RequestContext when rendering the template.
- [Django]-What is the difference between a cookie and a session in django?
- [Django]-How to exclude two conditions in query via Django ORM?
- [Django]-How do I return JSON without using a template in Django?
10👍
Most things have already been mentioned but to compile and add a little:
-
Make sure your settings.py file correctly specifies the locations of your static files. As Steven Keith wrote, you should have something like:
# Absolute path to the directory static files should be collected to. # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. # Example: "/home/media/media.lawrence.com/static/" STATIC_ROOT = '' STATIC_URL = '/static/' ADMIN_MEDIA_PREFIX = '/static/admin/' import os # Additional locations of static files STATICFILES_DIRS = ( os.path.join(os.path.dirname(__file__), 'static'), )
-
Then make sure that your TEMPLATE_CONTEXT_PROCESSORS include ‘django.core.context_processors.static’. If there’s no entry for TEMPLATE_CONTEXT_PROCESSORS in your settings.py, then it defaults to the below, and you’re all good.
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', )
-
Make sure that you use RequestContext when you render your templates.
django.shortcuts.render
does this by default (see here), so you could just callfrom django.shortcuts import render def myViewFunction(request): return render(request, 'myTemplate.html')
Be careful as django.shortcuts.render_to_response doesn’t do this for you unless you add an argument, so you’d have to write something like
from django.shortcuts import render_to_response def myViewFunction(request): return render_to_response('myTemplate.html', myContext, context_instance=RequestContext(request))
- [Django]-How do I output HTML in a message in the new Django messages framework?
- [Django]-In a Django template for loop, checking if current item different from previous item
- [Django]-Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty
9👍
For me, the answer was simply to stop using STATIC_URL, and instead use the following:
I changed mine from
<link href="{{ STATIC_URL }}css/survey.less" media="screen" rel="stylesheet" type="text/css"/>
to:
<link href="{% static "css/style.less" %}" media="screen" rel="stylesheet" type="text/less"/>
And now it works fine. Much simpler, and I suspect this is also the slightly “more correct” way to use static now (as of Django 1.4) anyways. Please see the Django docs for more info on the specifics.
Dont forget to add {% load static from staticfiles %}
at the top of your templates that use it too.
- [Django]-Why does django ORM's `save` method not return the saved object?
- [Django]-Virtualenv and source version control
- [Django]-Update/append serializer.data in Django Rest Framework
4👍
As I’ve been accused of not answering the question, let me clarify me thought process:
My question is why doesn't my first version of this template work?
STATIC_URL is returning a False value
To determine why, here is the steps I would use:
-
Try printing it in your templates body – {{ STATIC_URL }}.
-
Check settings.py to ensure the value is set – STATIC_URL = ‘/static/’
Check whether you have the STATIC files set up properly in runserver:
https://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/
For reference I use:
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
# Additional locations of static files
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
or an Apache alias:
Alias /static /Users/stevenkeith/foo/bar/static
<Directory /Users/stevenkeith/foo/bar/static>
Order deny,allow
Allow from all
</Directory>
or whatever method you want, nginx, etc
- [Django]-Django User model, adding function
- [Django]-How to set and get session in Django?
- [Django]-How to store IP address in the database and django admin
- [Django]-Manually logging in a user without password
- [Django]-Could not parse the remainder
- [Django]-Exclude fields in Django admin for users other than superuser
0👍
What worked for me was pointing the STATIC_ROOT and the STATIC_URL to the same static directory
my basic file structure
core/static/(
...
img/logos/green.jpg
js
css
...
)
…
STATIC_ROOT='/core/static/'
STATIC_URL='/core/static/'
and so in my html template I did
{% load static %}
<img src="{% static 'img/logos/green.jpg' %} />"
...
It worked!
- [Django]-What is the best way to migrate data in django
- [Django]-Django: display time it took to load a page on every page
- [Django]-Determine complete Django url configuration