fix STATIC_URL is empty in Django

When using static files in my Django application, the STATIC_URL is empty even I set it in the setting.py, after a few research, here is how I fixed it.

My static image files are located at movie/static/img (my application is movie)

1. in views.py

from django.template import RequestContext

if you use render_to_response, make sure you add context_instance=RequestContext(request) in it,

render_to_response('movie/index.html', {'movie_list': movie_list}, context_instance=RequestContext(request));

2. in settings.py, make sure there are following lines:

STATIC_URL = '/static/'

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. in template, the usage is:

<img src="{{ STATIC_URL }}img/{{ movie.image }}" />

Leave a Reply

Your email address will not be published. Required fields are marked *