title: Access MEDIA_URL from static Javascript files in Django
link: http://jj.isgeek.net/2008/11/access-media_url-from-static-javascript-files-in-django/
author: Jj
description: 
post_id: 464
date: 2008/11/21 19:37:36
created_gmt: 2008/11/22 00:37:36
comment_status: open
slug: access-media_url-from-static-javascript-files-in-django

This is an old trick, that's been very handy :) .

Many times for various reasons you'll need to access your media files from your Javascript files, to display images, change paths, or whatever. And in most cases your development `MEDIA_URL` and production `MEDIA_URL` will be different, so having to change them depending on the enviroment can be a bit tricky being them static files I'm assuming you are using static files your your .js right?.

What I do to help this is add the following to my base.html :
    
    
    
    
    
    
    <script type="text/javascript">
    
    
    
    
        var MEDIA_URL = "{{MEDIA_URL}}";
    
    
    
    
    </script>
    
    
    
    
    

before I load any of my .js files, and in them I simply refer to the `MEDIA_URL` variable _just_ as in my [Django](http://djangoproject.com/) templates.

Of course, you need to have your `settings.MEDIA_URL` variable on template context via whatever method you feel mor confortable with, personally I use context_processors :) .

## Comments

**[Walter Cruz](#240498 "2008-12-01 11:15:28"):** Hi. In a post here (http://www.fairviewcomputing.com/blog/2008/04/16/django-geography-hacks/) you talk about a geo plugabble app. Have you done that?

**[Jj](#240606 "2008-12-01 20:02:57"):** Contact me if you're interested, I could send you the code and probably we could work together

**[Abhas](#355475 "2010-11-23 07:49:26"):** On doing this I get an error MEDIA_URL is undefined inspite of the declaration.I would appreciate any help on the matter.

