Skip to content Skip to sidebar Skip to footer

Django: Static File Image URL Paths Breaks Except For Main Page Template

I'm trying to setup my static file in development. I have an image located in polls/static/images/banner.jpg. When I navigate to 127.0.0.1:8000/ the banner shows up, but when I go

Solution 1:

I think that in the second page, {{ STATIC_URL }} is not defined. Thus, src ends up with images/gcs_banner.jpg. Which is a relative url because it's not prefixed with a slash. It is then converted to a absolute path using the current absolute url: /2ndpage/images/gcs_banner.jpg.

{{ STATIC_URL }} is probably set by a context processor - at least that how it works in my projects. Context processors are actually a feature from RequestContext. When a view returns response without RequestContext then the context processors are not run, e.g.:

from django import shortcuts 
# ....
    return shortcuts.render_to_response(template_name, context)

This is an example of explicit usage of RequestContext with render_to_response():

from django import shortcuts 
from django import template
# ....
    return shortcuts.render_to_response(template_name, context, 
        context_instance=template.RequestContext(request))

That said, Django 1.3 provides a better shortcut with implicit usage of RequestContext, render():

from django import shortcuts 
# ....
    return shortcuts.render(request, template_name, context)

Post a Comment for "Django: Static File Image URL Paths Breaks Except For Main Page Template"