Login

media_url context variable

Author:
marchino
Posted:
April 11, 2007
Language:
Python
Version:
.96
Tags:
url media context
Score:
5 (after 5 ratings)

with this you can have context variables which know the media url and the urls of all your applications, if you need it.

save the code as myapp/context_processors.py and add the following line to TEMPLATE_CONTEXT_PROCESSORS setting

"mysite.myapp.context_processors.url_info",

For each application you need to know the url set MYAPP_URL and add it to dict.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from django.conf import settings

def url_info(request):
    """Return useful variables to know the media url and, if you
    need it, your apps url."""

    return {
        'myapp_url': settings.MYAPP_URL,
        'media_url': settings.MEDIA_URL,
    }

More like this

  1. Subdirectory and subcontext include template tag with examples by t_rybik 5 years, 2 months ago
  2. Cycling MEDIA_URL context processor by girasquid 6 years, 5 months ago
  3. Private Context Decorator by acdha 5 years, 9 months ago
  4. Arbitrary length formset by Rupe 5 years, 8 months ago
  5. Serve static media and indexes from app directories [Python2.5, Development only] by adamlofts 6 years, 9 months ago

Comments

jwheare (on April 11, 2007):

I've been using a similar context processor for a while now and one caveat to be aware of is that error 500 templates aren't rendered with request context by default. This means that context processors are not applied and may lead to broken media references (style sheets/images).

You can work around this by pointing to your own handler500 view from your urlconf and using RequestContext instead of Context to render the template.

#

Please login first before commenting.