Custom Template Tag - What do I need in my actual template?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom Template Tag - What do I need in my actual template?

MerMer

I'm steadily working through the excellent custom template tag
tutorial:-

http://www.b-list.org/weblog/2006/06/07/django-tips-write-better-template-tags

I've got the following code to create a template tag.  However,  I'm
unclear as to what
I need to put into my actual template.

The author states, that based on the code below,  I would need to add
{% get_latest_polls 3 %} to the template, where 3 is a variable.
However, while this not not create any errors, it is also not returning
the latest 3 Polls.   What else do I need to add?

MerMer

from django import template
from mysite.polls.models import Poll, Choice

class LatestPollsNode(template.Node):
    def __init__(self,num):
        self.num = num
    def render(self, context):
        context['polls'] = Poll.objects.all()[:self.num]
        return ''

def get_latest_polls(parser, token):
    bits = token.contents.split()
    if len(bits) !=2:
        raise TemplateSyntaxError, "get latest_latest_polls tag take
exactly one arguement"
    return LatestPollsNode(bits[1])
   
get_latest_polls = register.tag(get_latest_polls)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Custom Template Tag - What do I need in my actual template?

Tim Shaffer

Your code looks valid as it is right now. The get_latest_polls function
should return a QuerySet of the three latest polls.

When you call this function it will get the polls and assign the
QuerySet to the "polls" variable for use in your template.

In order to display the polls, you would need to do something like
this:

{% get_latest_polls 3 %}

{% for poll in polls %}
    {{ polls.question }}
{% endfor %}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Custom Template Tag - What do I need in my actual template?

MerMer

Tim,

Many thanks - that works a treat.  I was getting myself very confused
about where and how to use the context.

Cheers

MerMer


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---