【问题标题】:Calling a function from another html template从另一个 html 模板调用函数
【发布时间】:2022-01-06 19:41:19
【问题描述】:

所以我有一个base.html 模板和一个扩展base.html 模板的review_result.html 模板。 base.html 模板有一个脚本标签,有很多功能。我将把名为format 的函数从review_result.html 中的脚本标记移动到base.html 文件。然后如何从base.html 调用review_result.html 中的format 函数?

【问题讨论】:

  • 你说你的base.html一个脚本标签是什么意思?一般来说,模板不包含功能代码或标签。可用的标签要么是内置的,要么是由您的站点配置控制的,或者是使用{% load %} 加载的——在任何情况下,它们都不在您的模板内部。见custom template tags。另外,这是否回答了您的问题:Is it possible to load a custom template tag in base and use it in extented templates ?
  • 你好,欢迎来到 SO。这是一个很好的问题,但您可以编辑您的帖子并添加您尝试过的任何代码吗?它肯定会帮助任何答案。

标签: python html django templates django-templates


【解决方案1】:

Django 模板只是变成了静态 html(包括脚本标签)。如果您从另一个模板扩展并且它包含您的 JavaScript,那么您应该能够调用该函数,就像您的 format 函数在您的 review_result.html 模板中一样。

【讨论】:

    猜你喜欢
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    相关资源
    最近更新 更多