python - 动态模板标签

标签 python django django-templates

我有自己的模板标签:

@register.inclusion_tag('path_to_module.html', takes_context=True)
def getmodule(context, token):
    try:
        return slow_function(params)
    except Exception, e:
        return None

而且速度非常慢。模板正在等待此标签。

我可以异步调用它们吗?

最佳答案

如果它是可缓存的(不需要每个页面 View 都是唯一的);然后缓存它。使用Django's cache API在您的模板标签中,或 template fragment caching直接在您的模板中。正如 @jpic 所说,如果需要一段时间才能重新计算,请将其传递给像 Celery 这样的任务队列。

如果您出于某种原因需要此功能来运行每个页面 View ;然后将其分离到一个新 View 中,并使用 JavaScript 将其异步加载到主模板中的某个容器中。

关于python - 动态模板标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9805233/

相关文章:

python - 为什么 django 提示我还没有设置我的引擎?

python - 如何使用python将json存储在django mysql数据库中?

python - Python最快的模板系统是什么?

python - 设置 Django 博客评论

python - 实现部分c源代码

python - 使用 Intellij 和 Scala 安装和使用库

python - 装饰器中的 Django Model._meta.get_fields()

Django spaceless 模板标签优点

python - 从python中的多值元素数组打印特定值

python - 如何使用 Django ORM 排除列的多个值?