[Answered ]-Where put database interaction in django templatetags

2👍

Template tags shouldn’t interact with your database at all, really. At least not in the sense that you are interacting with your model/business data. This would violate the separation of concerns that is one of the main reasons to use an MVC (er, MTV) style framework in the first place.

If you’re talking about a data-driven template tag that interacts with the database for some reason to deal with only presentation-level stuff, then it should go in a method within your Node that is called within your Node‘s render method.

class MyCustomNode(template.Node):
    def __init__(self, ...):
        ...

    def render(self, context):
        # do your db lookup here
        return some_string_using_the_db_stuff

Leave a comment