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 .
{% 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 !
Edit the template html file :
{% 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)
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 .
Regards H. M.
There's an option called 'Templates' with another option to 'Toggle context' and you can see all the variables passed to your template, as well as the ability to see the code behind the template. Save this answer.
These are special tokens that appear in django templates. You can read more about the syntax at the django template language reference in the documentation. {{ foo }} - this is a placeholder in the template, for the variable foo that is passed to the template from a view.
Django Template Engine provides filters which are used to transform the values of variables;es and tag arguments. We have already discussed major Django Template Tags. Tags can't modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one's own need.
{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.
You don't put the brackets around variables when you use them in template tags.
{% get_latest_photo photo.id %}
To evaluate correctly the num variable I think you should modify your LatestPhotoNode class like this:
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 ''
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With