Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STATIC_URL not working

Tags:

django

I am struggling to pull media out for my templates using the STATIC_URL variable. For example I have this code

{% extends "admin/change_list.html" %}
{% load i18n %}

{% block extrahead %}
<!--[if IE]>
<script type="text/javascript" src="{% firstof STATIC_URL MEDIA_URL %}django_qbe/js/excanvas.js"></script>
<![endif]-->
<script type="text/javascript" src="{% firstof STATIC_URL MEDIA_URL %}django_qbe/js/jquery.js"></script>

Each time the template loads it tries to pull off the MEDIA_URL. If I change it to

{% extends "admin/change_list.html" %}
{% load i18n %}
{% load static %}
{% block extrahead %}
<!--[if IE]>
<script type="text/javascript" src="{% get_static_prefix %}django_qbe/js/excanvas.js"></script>
<![endif]-->
<script type="text/javascript" src="{% get_static_prefix %}django_qbe/js/jquery.js"></script>

My question is why doesn't my first version of this template work?

like image 343
darren Avatar asked Jun 07 '11 07:06

darren


4 Answers

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...

like image 144
John C Avatar answered Nov 20 '22 02:11

John C


From the docs:

If {{ STATIC_URL }} isn't working in your template, you're probably not using RequestContext when rendering the template.

https://docs.djangoproject.com/en/dev/howto/static-files/

like image 30
Jon Vaughan Avatar answered Nov 20 '22 02:11

Jon Vaughan


Most things have already been mentioned but to compile and add a little:

  1. 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'),
    )
    
  2. 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',
    )
    
  3. Make sure that you use RequestContext when you render your templates. django.shortcuts.render does this by default (see here), so you could just call

    from 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))
    
like image 10
embirico Avatar answered Nov 20 '22 03:11

embirico


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.

like image 9
The NetYeti Avatar answered Nov 20 '22 04:11

The NetYeti