Django-Template : Get Variables in a Tag block !

前端 未结 4 632
有刺的猬
有刺的猬 2021-02-04 14:37

I need to retrieve an optional number saved in DB , to a custom template tag i made . which to retrieve , a variable ( a photo ID ) included in this Gallery . within the gallery

4条回答
  •  花落未央
    2021-02-04 14:54

    To evaluate correctly the num variable I think you should modify your LatestPhotoNode class like this:

    class LatestPhotoNode(Node):
        def __init__(self, num):
            self.num = template.Variable(num)
    
        def render(self, context):
            num = self.variable.resolve(self.num)
            photo = Photo.objects.filter(akar=num)[:1]
            context['recent_photos'] = photo
            return ''
    

提交回复
热议问题