Hello all.
I'm planning to host my Django app with SSL optional, so that the same
content can be accessed via http or https addresses.
I'm hosting my static content on a different sub-domain, so I have to
use absolute addresses in my img/link tags. I was looking for a solution
to the problem of sending the right addresses depending on whether the
page in question is going out over a SSL connection or not (to avoid the
dreaded mixed-content warnings).
The solution I came up with is a context processor which processes
constants stored in the settings file:
TEMPLATE_CONTEXT_PROCESSORS = (
'securityvars.security_vars',
)
SECURITY_VARS = {
'main':(
'http://example.org',
'https://secure.example.org'),
'static':(
'http://static.example.org',
'https://secure.static.example.org:444'),
'switch_modes':(
'Go secure',
'Go insecure'),
}
The context processor places into the context instances of a class
SecurityVar that correspond with the key names given in SECURITY_VARS.
These can then be used in templates like this:
<img src="{{ static }}/logo.png"/>
and the correct one will be substituted depending on whether the request
is_secure() or not. You can also specify in the tag that you definitely
want the secure or insecure version, so a link to a login page might
look like this:
<a href="{{ main.secure }}/login">Login</a>
The SecurityVars also have an opposite() function that returns the
'other' variable; that is, if the connection is secure it returns the
insecure one, and if the connection is insecure it returns the secure
one. This can be used like this:
<a href="{{ main.opposite }}/">{{ switch_modes }}</a>
The implementation is attached. It may not be the best as I'm relatively
new to Python and very new to Django (so I'm terrified that there's
already a simple way of doing this that I just haven't discovered yet ;-)
Any comments? Is this general-purpose and useful enough to be included
with Django? In addition to this one, I think there'd also be a case for
a one-liner context processor for site-wide variables that just takes a
dictionary from settings and adds it to the context.
Thanks.
--
Jon
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django
developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---
from django.conf import settings
def security_vars(request):
class SecurityVar:
def __init__(self, items):
(self.insecure, self.secure) = items
def choose(self):
return self.secure if request.is_secure() else self.insecure
def opposite(self):
return self.insecure if request.is_secure() else self.secure
def __str__(self):
return str(self.choose())
def __unicode__(self):
return unicode(self.choose())
d = {}
for k, v in settings.SECURITY_VARS.iteritems():
d[k] = SecurityVar(v)
return d