how to django custom template tab
----------------in template
{% can_edit_comment value comment.created_on %}
{% if value %}
hi
{% endif %}
----------------in /templatetags/siren.py
class CanEditCommentNode(template.Node):
def __init__(self ,tag_name, variable, created_on):
self.variable = variable
self.tag_name = tag_name
self.created_on = created_on
def render(self, context):
created = template.resolve_variable(self.created_on, context)
edit_time = created + datetime.timedelta(minutes=55)
print "today", datetime.datetime.today()
if edit_time > datetime.datetime.today():
print "can edit"
context[self.variable] = True
else:
print "not edit"
context[self.variable] = False
return ''
@register.tag
def can_edit_comment(parser, token):
try:
tag_name, variable, created_on = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError
return CanEditCommentNode(tag_name, variable, created_on)
------------------------
No comments:
Post a Comment