Login

Use MEDIA_URL in 500 error page

Author:
bthomas
Posted:
November 17, 2008
Language:
Python
Version:
1.0
Tags:
media 500 handler servererror
Score:
5 (after 5 ratings)

The default server_error view uses Context instead of RequestContext. If you were depending on a context processor to make MEDIA_URL available in your templates, your 500.html template will not render with the correct image paths. This handler adds MEDIA_URL (and nothing else) back to the context that is sent to the template.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# views.py
from django.conf import settings
from django.template import Context, loader

def server_error(request, template_name='500.html'):
    "Always includes MEDIA_URL"
    from django.http import HttpResponseServerError
    t = loader.get_template(template_name)
    return HttpResponseServerError(t.render(Context({'MEDIA_URL': settings.MEDIA_URL})))

# urls.py
handler500 = 'views.server_error'

More like this

  1. A custom 500 handler which is AJAX-aware by mallipeddi 7 years, 5 months ago
  2. Use MEDIA_URL in flatpages with SSL by gobble 5 years ago
  3. Use MEDIA_URL in flatpages by robhudson 7 years, 3 months ago
  4. Cycling MEDIA_URL context processor by girasquid 6 years, 7 months ago
  5. Load static media from secure (SSL) static server (Context Processor) by ianreardon 5 years, 8 months ago

Comments

Please login first before commenting.