Django-Template : Get Variables in a Tag block !

前端 未结 4 577
有刺的猬
有刺的猬 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 15:07

    I had the same problem problem and after reading the docs, I solved it using this

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

    If you are trying to render multiple variables, using json.dumps is very useful.

提交回复
热议问题