나는 DB에 저장된 선택적인 번호를 내가 만든 커스텀 템플리트 태그로 가져와야한다. 이 갤러리에 포함 된 변수 (사진 ID)를 검색합니다. 갤러리 루프 내에서.Django-Template : 태그 블록에서 변수 가져 오기!
{% 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 %}
templatetag 숫자로 ({% get_latest_photo photo.id %}에서) 내가 album.id 교체 할 때 매우 잘 작동
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)
시 그 앨범 ID로 작동하고 사진을 검색합니다. 당신은 템플릿 태그를 사용할 때
감사 H. M.는
코드가 잘못 표시되는 방법을 알려 주시면 답변을 제공하는 것이 더 쉬울 것입니다. –
태그의 코드를 게시 할 수 있습니까? –