Wednesday 15 May 2013

How to write better template tags in django -


I have seen how I can write a template tag that defines a reference variable based on a template

<<> {% my_template_tag 'blah' my_context_variable%} as

but I want to be able to:

Given that Both groups and are set in the context in the user view

  {% is_in_group group user is_member%} {% if_member%} #. ... Do the work .... {% endif%}  

Or something like this:

  {% if_in_group group user%} # .. .. {% end if%}  

Obviously the second way is to set the only is_member in view - but this is just an example and will know Well how to do something like that!

Evgeny is a good idea tag with smart_ef template. However, if that does not work, you probably find that writing a custom filter for this type of comparison is easy. Something like this:

  @ register.filter def is_in (obj, val): Return Val is in obj  

and you will use it like this:

  {% if user | Is_in: group.users%}  

No comments:

Post a Comment