Login

ifsecure

Author:
kyprizel
Posted:
December 24, 2007
Language:
Python
Version:
.96
Tags:
templatetag is_secure https
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. ExcelResponse2 by Hackathon 11 months ago
  2. Django cycle tag that doesn't continue where it left off by wilfred 2 years, 2 months ago
  3. Jquery ajax csrf framework for Django by chriszweber 3 years, 2 months ago
  4. Email Auth by petzah 11 months, 1 week ago
  5. Load static media from secure (SSL) static server (Context Processor) by ianreardon 5 years, 5 months ago

Comments

Please login first before commenting.