开发者

calling python function from django template tags

开发者 https://www.devze.com 2023-02-16 00:40 出处:网络
How can we pass a variable from a django template tag i.e, i want to do something like this {{emp.get_names(\'a\')}}

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开发者_如何学Go=True)
   address1 = models.CharField(max_length=255)

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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消