views:

34

answers:

2

I have a development server running (and serving content) using the built in django server. All my templates that are rendered from generic views point correctly to the static media files (css/java/imgs) but ones that are rendered via custom views don't seem to prepend the /media/ folder to the urls. (At least this seems to be the problem)

In my settings I have:

DJANGO_PATH = os.path.realpath(os.path.dirname(__file__))
DB_PATH = os.path.join( (os.path.split(DJANGO_PATH))[0] , 'db/dev.db')
TEMPLATE_PATH = os.path.join( DJANGO_PATH , 'templates')
DEBUG = True
TEMPLATE_DEBUG = DEBUG

MEDIA_PATH = os.path.join( (os.path.split(DJANGO_PATH))[0] , 'media')
ADMIN_MEDIA_PREFIX = '/media/admin/'
MEDIA_URL = '/media/'
MEDIA_ROOT = MEDIA_PATH

and In my urls I have an entry

(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True }),

Anyone got any ideas?

EDIT:
Ooops, forgot to mention. All my templates inherit from a base template which has all the media files like:

{{ MEDIA_URL }}css/some/file.css

So in my templates folder I have:

/templates/base.html
/templates/someapp/childtemplate.html

with all the css/js in the header like above. Then in the templates specific to my applicaiton I am simply inheriting the base template

Furthermore
I can view the media by visiting

localhost:8000/media/  

no problem, so the urlCONF seems to be doing it's job

+1  A: 

If I'm not mistaken, MEDIA_URL is not accessible by default from the template context. The easiest workaround is to create a new template context processor:

def media_url(request):
    from django.conf import settings
    return {'media_url': settings.MEDIA_URL} 

And in settings.py:

TEMPLATE_CONTEXT_PROCESSORS = ('myapp.context_processors.media_url',)

Just remember to use the RequestContext when rendering the template. In the view:

from django.template import RequestContext
return render_to_response("my_app/my_template.html", {'some_var': 'foo'}, context_instance=RequestContext(request))

Your templates should now render correctly.

For more on this: http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/

OmerGertel
+3  A: 

Make sure that django.core.context_processors.media is in your TEMPLATE_CONTEXT_PROCESSORS variable in settings.py. This enables the use of {{ MEDIA_URL }} in your templates.

Edit: As Daniel pointed out, I forgot to mention that yes, in order to take advantage of your TEMPLATE_CONTEXT_PROCESSORS you need to pass context_instance=RequestContext(request) to the render_to_response function in your view. Some alternatives to having to do that for every view are to use the render_to() decorator in the django-annoying third-party app, or to just import and use direct_to_template instead of render_to_response, which is a neat trick in that it automatically uses the RequestContext.

Pewpewarrows
I forgot about django.core.context_processors.media. This is, of course, better than what I've suggested.
OmerGertel
You still need to ensure you use `context_instance=RequestContext(request)` as noted in Omer's answer below.
Daniel Roseman
Having django.core.context_processors.media in the settings, along with Omer's answer has solved it. If I was using a production server, the media would be dealth with by apache/cherokee. I presume this would mean I wouldn't need the django context processor for dealing with media?
pastylegs
Yes you still need the context processor in production. The context processor, along with using RequestContext, is how you get {{ MEDIA_URL }} into your templates.
Brian Neal