Login

Load static media from secure (SSL) static server (Context Processor)

Author:
ianreardon
Posted:
October 9, 2009
Language:
Python
Version:
1.1
Tags:
secure media_url processor content ssl files static context
Score:
1 (after 1 ratings)

If you request static files such as images, javascript or css using http rather than https, the browser will complain that your site is not secure.

This simple context processor will replace http:// with https:// in your MEDIA_URL if your static files are being included from an https page.

In your settings.py just replace 'django.core.context_processors.media' with your new context processor.

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

def ssl_media(request):

  if request.is_secure():
    ssl_media_url = settings.MEDIA_URL.replace('http://','https://')
  else:
    ssl_media_url = settings.MEDIA_URL

  return {'MEDIA_URL': ssl_media_url}

More like this

  1. Use MEDIA_URL in flatpages with SSL by gobble 5 years, 2 months ago
  2. Cycling MEDIA_URL context processor by girasquid 6 years, 9 months ago
  3. SSL / HTTPS Middleware for Redirection and href Rewriting by DrMeers 5 years, 4 months ago
  4. SSL Middleware by sjzabel 8 years, 6 months ago
  5. Use MEDIA_URL in flatpages by robhudson 7 years, 5 months ago

Comments

ianreardon (on October 9, 2009):

Should of put that this is a Context Processor in the title.

#

rpoulton (on October 10, 2009):

Why not just refer to your media using img src='//media.mydomain.com/image.jpg'?

Removing the http: from the URL will force the browser to request using either HTTP or HTTPS, based on the current request. It's just taking the relative path a step further - down to the protocol, not just the hostname.

#

Please login first before commenting.