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
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month, 4 weeks ago
 - get_object_or_none by azwdevops 5 months, 2 weeks ago
 - Mask sensitive data from logger by agusmakmun 7 months, 2 weeks ago
 - Template tag - list punctuation for a list of items by shapiromatron 1 year, 9 months ago
 - JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 9 months ago
 
Comments
Please login first before commenting.