Ho bisogno di recuperare un numero opzionale salvato in DB, su un tag modello personalizzato che ho creato. quale recuperare, una variabile (un documento d'identità) inclusa in questa galleria. all'interno del ciclo della galleria.Template Django: ottieni variabili in un blocco di tag!
{% get_latest_photo {{photo.id}} %}
Come eseguire ciò?!
P.s: So che può essere fatto con il tag di inclusione, ma nel tempo presente come farlo risolvere questo!
Modificare il file template 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)
Ps Il suo lavoro molto bene quando i sostituire album.id (in {% get_latest_photo photo.id%}) con un numero che funge da id per l'album e recupera la foto da.
saluti H. M.
Se ci dici come il codice si comporta male, sarà più facile per noi darti delle risposte. –
Puoi pubblicare il codice del tag? –