Custom Template Tags Render Method

9 views
Skip to first unread message

Ross

unread,
Nov 24, 2019, 1:43:44 PM11/24/19
to Django users
To create our own custom template tags we have to define a Node subclass which implements a render method.


import datetime
from django import template

class CurrentTimeNode(template.Node):
    def __init__(self, format_string):
        self.format_string = format_string

    def render(self, context):
        return datetime.datetime.now().strftime(self.format_string)



It seems the context parameter for the render function is a 'context object' and not a 'request context object'.  Indeed this section of the same article seems to support this - To create our own custom template tags we have to define a Node subclass which implements a render method.

Anyway when I followed the example and printed the context object it looks to be a request object.  Is this a bug?  I'm using the latest django.

Ross

unread,
Nov 24, 2019, 3:48:57 PM11/24/19
to Django users
I meant to write "indeed this section of the same article seems to support this - https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/#auto-escaping-considerations"
Reply all
Reply to author
Forward
0 new messages