在标记块中获取变量!

时间:2023-01-10 20:21:44

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 loop .

我需要检索一个在DB中保存的可选数字,到我所做的自定义模板标记。要检索的变量(照片ID)包含在此图库中。在画廊圈内。

{% get_latest_photo   {{photo.id}}  %} 

How to accomplish that ?!

怎么做到呢?!

P.s : I know that can be done with inclusion tag , but in the present time how to make it fix this one !

P。s:我知道包含标签可以做到这一点,但是在目前的时代如何使它修复这个标签呢!

Edit the template html file :

编辑模板html文件:

{% for album in albumslist %}

    {% get_latest_photo   photo.id  %} 
    {% for photo in recent_photos %}
<img src='{% thumbnail photo.image 200x80 crop,upscale %}' alt='{{ photo.title }}' />
    {% endfor %}
    {{ album.title }}
{% endfor %}

templatetag

templatetag

from django.template import Library, Node
from akari.main.models import *
from django.db.models import get_model

register = Library()

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

def get_latest_photo(parser, token):
    bits = token.contents.split()
    return LatestPhotoNode(bits[1])

get_latest_photo = register.tag(get_latest_photo)

P.s Its working very well when i replace album.id (in {% get_latest_photo photo.id %} ) with a number which acts as an album id and retrieve the photo from .

P。it’我换新专辑的时候效果很好。id(在{% get_latest_photo照片中。id %})具有一个作为相册id并从其中检索照片的数字。

Regards H. M.

把h . M。

4 个解决方案

#1


5  

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

为了正确地评估num变量,我认为您应该修改您的LatestPhotoNode类如下:

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 ''

#2


8  

You don't put the brackets around variables when you use them in template tags.

当您在模板标签中使用时,您不需要将括号括起来。

{% get_latest_photo photo.id %}

#3


3  

Are you sure your template tag is written properly? For example, you need to use Variable.resolve to properly get the values of variables: Passing Template Variables to the Tag

你确定你的模板标签写的正确吗?例如,您需要使用变量。要正确地获取变量的值:将模板变量传递给标记。

#4


1  

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.

如果您正在尝试使用json呈现多个变量。转储是非常有用的。

#1


5  

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

为了正确地评估num变量,我认为您应该修改您的LatestPhotoNode类如下:

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 ''

#2


8  

You don't put the brackets around variables when you use them in template tags.

当您在模板标签中使用时,您不需要将括号括起来。

{% get_latest_photo photo.id %}

#3


3  

Are you sure your template tag is written properly? For example, you need to use Variable.resolve to properly get the values of variables: Passing Template Variables to the Tag

你确定你的模板标签写的正确吗?例如,您需要使用变量。要正确地获取变量的值:将模板变量传递给标记。

#4


1  

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.

如果您正在尝试使用json呈现多个变量。转储是非常有用的。