Henry,

I do the same thing in my project.  There are a couple of ways you can use
to expose the media url.  If you won't be using the it very often, you can
do the following:

from django.shortcuts import render_to_response
from django.conf import settings

def example(request):
    return render_to_response('example.html', {'media_url':
settings.MEDIA_URL})

Then, in your template, you would access the media url like any other
variable:

<img src="{{ media_url }}/image.gif">

The second way is to create a custom context processor.  I find it easier to
do it this way because the media url is auto populated for me and I don't
have to remember to do it for every response.  Here's an example:

First, create a context_processors.py file at the base of your app. You can
call this whatever you want and place it wherever you want as long as django
can find it later.

In that file, create a function to populate the context with the relevant
information:

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

You then have to let django know how to find the context processor you just
wrote.  This is done in your settings.py file using the
"TEMPLATE_CONTEXT_PROCESSORS" value (the 'path.to' portion should be
replaced with your project specific info):

TEMPLATE_CONTEXT_PROCESSORS = ('path.to.context_processors.media_url' ,)

To use the context processor, you have to include a context with your
response information:

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

def example(request):
    context = RequestContext(request)
    return render_to_response('example.html', context_instance=context)

Now the media url is available in your templates:

<img src="{{ media_url }}/image.gif">

More information about the RequestContext can be found
here<http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext>
.

Let me know if you have any questions.

Sean


On 3/2/07, Henrik Lied <[EMAIL PROTECTED]> wrote:
>
>
> Hi there,
>
> I'm just wondering what the best way of retrieving the MEDIA_URL for
> an object is.
>
> Let's say I'm writing a blog post with some images. Instead of writing
> <img src=" http://media.mydomain.com/image.gif";>, I'd write <img
> src="_MEDIA_URL_/image.gif"> or something equivalent.
>
> So if I change my domain, I wouldn't have to rewrite a ton of blog
> posts to change the domain.
>
> ----------
>
> Have anyone tried this?
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to