我需要檢索保存在數據庫中的可選編號,以製作我自定義的模板標籤。它將檢索包含在此圖庫中的變量(照片ID)。在畫廊循環內。Django-Template:在變量塊中獲取變量!
{% get_latest_photo {{photo.id}} %}
如何做到這一點? P:我知道可以用包含標籤來完成,但是現在如何使它修復這個問題!
編輯模板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
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)
詩及其與一些工作得很好,當我更換album.id(在{%get_latest_photo photo.id%})它充當專輯ID並從中檢索照片。
問候 H. M.
如果您告訴我們代碼的錯誤行爲,我們會更容易給您答案。 – 2009-08-13 01:40:01
你可以發佈標籤的代碼嗎? – 2009-08-13 02:17:16