In some cases we need to know if we were opened via https from template.
Usage: {% ifsecure %}using https{% else %}not using https{% endifsecure %}
If you use fastcgi fastcgi_param HTTPS must exists.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class IfSecureNode(template.Node):
def __init__(self, nodelist_true, nodelist_false):
self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
def __repr__(self):
return ""
def render(self, context):
request = context.get('request', None)
if request is None:
return self.nodelist_false.render(context)
if request.is_secure():
return self.nodelist_true.render(context)
return self.nodelist_false.render(context)
def do_ifsecure(parser, token):
bits = list(token.split_contents())
end_tag = 'end' + bits[0]
nodelist_true = parser.parse(('else', end_tag))
token = parser.next_token()
if token.contents == 'else':
nodelist_false = parser.parse((end_tag,))
parser.delete_first_token()
else:
nodelist_false = template.NodeList()
return IfSecureNode(nodelist_true, nodelist_false)
@register.tag
def ifsecure(parser, token):
return do_ifsecure(parser, token)
register.tag('ifsecure', ifsecure)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.