Rajeev
Rajeev

Reputation: 46909

calling python function from django template tags

How can we pass a variable from a django template tag i.e, i want to do something like this

  {{emp.get_names('a')}}

emp is the object that i am passing from my views

 class Emp(models.Model):
   name = models.CharField(max_length=255, unique=True)
   address1 = models.CharField(max_length=255)

   def get_names(self,var):
      logging.debug(var)          
      names = {}

Upvotes: 0

Views: 2505

Answers (3)

Kenzic
Kenzic

Reputation: 525

Django templates are designed to prevent people from doing what you are trying to do. Use a template tag.

Upvotes: 1

soasme
soasme

Reputation: 392

It seems that it is not support calling the function in the default template like this.
maybe you can use some built-in tags like {{'a'|get_names}}
You can try to use Jinja2 template,which can let you write python code in it.

Upvotes: 0

Mp0int
Mp0int

Reputation: 18727

You can not call a function that takes a parameter like that. Maybe writing a custom template tag can help, but, why do you need to do it in the template, but not in the view??

Custom template tags

Upvotes: 4

Related Questions