Ross Burton wrote:
> then in the view:
> 
> {% for paper in object_list|dictsort:"title" %}
>   {% if paper.has_voted( TODO ) %}
>     <!-- todo: set different css class -->

Not related to your question, but this is called 'template' in Django. 
'View' means a different thing (a controller).

> I then discovered that you can't pass arguments to methods in
> templates, so I can't ask the model if the current user has voted.

I'm sure someone will suggest something less scary for a beginner but I 
can now only come up with such a custom template tag:

     class IfVotedNode(template.Node):
       def __init__(self, paper_expr, node_list):
         self.paper_expr, self.node_list = obj_expr, node_list

       def render(self, context):
         paper = self.paper_expr.resolve(context)
         if paper.has_voted(context['user']):
           return self.node_list.render(context)
         else:
           return ''

     @register.tag
     def ifvoted(parser, token):
       bits = token.contents.split()
       if len(bits) != 2:
         raise template.TemplateSyntaxError, '"%s" takes 1 parameter ' % 
bits[0]
       node_list = parser.parse('end' + bits[0])
       parser.delete_first_token()
       return IfVotedNode(parser.compile_filter(bits[1]),node_list)

It's then used like this:

     {% ifvoted paper %}...{% endifvoted %}

Docs on creating custom template tags are here: 
http://www.djangoproject.com/documentation/templates_python/#writing-custom-template-tags

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

Reply via email to