本文介绍了如何调用在 Django 模板中带有参数的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在向 Django 的模板传递一个函数,该函数返回一些记录.我想调用这个函数并迭代它的结果.
I'm passing to Django's template a function, which returns some records.I want to call this function and iterate over its result.
{% for item in my_func(10) %}
那行不通.我尝试将函数的返回值设置为变量并迭代该变量,但似乎无法在 Django 模板中设置变量.
That doesn't work.I've tried to set the function's return value to a variable and iterate over the variable, but there seems to be no way to set a variable in a Django template.
有什么正常的方法可以做到吗?
Is there any normal way to do it?
推荐答案
不能在模板中调用需要参数的函数.编写模板标签或过滤.
You cannot call a function that requires arguments in a template. Write a template tag or filter instead.
这篇关于如何调用在 Django 模板中带有参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!