开发者

Where put database interaction in django templatetags

开发者 https://www.devze.com 2023-02-12 03:54 出处:网络
I\'m writing a simple templatetag that gets one item and call one开发者_运维百科 of its methods to get and prepare some data. And I wonder where I should call it, in Node or in compiler?

I'm writing a simple templatetag that gets one item and call one开发者_运维百科 of its methods to get and prepare some data. And I wonder where I should call it, in Node or in compiler? Thanks for advices!


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
0

精彩评论

暂无评论...
验证码 换一张
取 消