Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-Template : Get Variables in a Tag block !

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.

like image 838
Hamza Avatar asked Aug 12 '09 23:08

Hamza


People also ask

How can you display all variables in the page on to a Django template?

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.

What does {{ this }} mean in Django?

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.

Can I use filter () in Django template?

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.

What does {% include %} do?

{% 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.


4 Answers

You don't put the brackets around variables when you use them in template tags.

{% get_latest_photo photo.id %}
like image 114
Steve Losh Avatar answered Oct 19 '22 07:10

Steve Losh


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 ''
like image 23
Pierre-Jean Coudert Avatar answered Oct 19 '22 09:10

Pierre-Jean Coudert


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

like image 3
Ned Batchelder Avatar answered Oct 19 '22 08:10

Ned Batchelder


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.

like image 1
Pandikunta Anand Reddy Avatar answered Oct 19 '22 08:10

Pandikunta Anand Reddy