Login

ifsecure

Author:
kyprizel
Posted:
December 24, 2007
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

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

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

Please login first before commenting.