functions in django templates

半腔热情 提交于 2019-12-11 13:38:13

问题


Does django templates support functions?
Can I write them inline within the template?
If I can, how?


回答1:


No. The idea of Django templates is that they only display information (more or less). More complicated constructs (like functions) must be defined elsewhere.

But, you can create your own template tags or template filters (in Python code) and then use them in your template. They kind of act like functions. Their definition is not within your template, though.

You can read about it in django documentation




回答2:


You can call method without arguments: stackoverflow answer

You can do it by using (.) dot notation. read more about variables in Django reference

Use a dot (.) to access attributes of a variable. Technically, when the template system encounters a dot, it tries the following lookups, in this order:

Dictionary lookup
Attribute lookup
Method call
List-index lookup


来源:https://stackoverflow.com/questions/5242029/functions-in-django-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!