← blog

FIX: {{ STATIC_URL }} not working in Django

In Django application development, a common practice is to specify a URL prefix for fetching static files in a variable named “STATIC_URL” in settings.py. However, if you start off with a Django project and forget to setup a RequestContext with your view, the STATIC_URL will not render in your template and your output would get broken. Here is a quick fix.

from django.shortcuts import render_to_response
from django.template import RequestContext

def about(request):
    return render_to_response("about.html",
        {'page_title': 'About us',
#and other stuff
         },
        RequestContext(request))

So, the trick is to add a RequestContext as a third argument to the render_to_response function. The STATIC_URL variable will not be available without it.