【发布时间】:2010-11-19 03:35:36
【问题描述】:
我需要检索一个保存在 DB 中的可选数字,到我制作的自定义模板标签中。要检索的对象是此图库中包含的变量(照片 ID)。在画廊循环中。
{% get_latest_photo {{photo.id}} %}
如何做到这一点?!
P.s:我知道可以使用包含标签来完成,但目前如何让它修复这个!
编辑模板 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 %}
模板标签
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 当我将 album.id(在 {% get_latest_photo photo.id %} 中)替换为一个充当相册 ID 并从中检索照片的数字时,它的工作非常好。
问候 H.M.
【问题讨论】:
-
如果您告诉我们代码的异常行为,我们将更容易为您提供答案。
-
能贴出标签的代码吗?
标签: django templates django-templates