python - 在html中添加python变量

标签 python html django django-templates

我正在尝试在 HTML 中嵌入的 python 中添加变量,它只是打印出添加语句而不是执行它们。这是我的 html 文件中的代码:

<% x = 0 %>
{% for c in UGC %}
  {% if c.doctor_id == doctor.id  %}
    <% {{x}} = {{x}} + {{c.time}} %>
    <h4>Around {{c.time}} minutes.</h4>
    <h4>{{c.comment}}</h4>
    <h4>{{c.submitted_on}}</h4>
    <br>
  {% endif %}
{% endfor %}

{{c.time}} 从数据库打印出实际时间。 {{c.comment}} 和 {{c.subscribed_on}} 分别从数据库中打印出相关评论和时间戳。但是,当我将 x 初始化为 0,然后尝试在循环的每次迭代中为其添加时间时,将打印以下内容: '<% = + 33 %>' 其中 33 是​​ {{c.time}} for 的值循环的迭代。

最佳答案

1 - 您正在混合 PHP 模板和 Django 模板。

2 - 如果您在此处使用 x 作为临时变量,则可以与模板标签一起使用。

{% with x = 0 %}

/// you code here

{% endwith %}

3 - 此行 <% {{x}} = {{x}} + {{c.time}} %>永远不会起作用。您必须编写自己的模板标签才能添加。 {% x|add:c.time %}

关于python - 在html中添加python变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24212288/

相关文章:

python - 如何在 django-tables2 中调用非模型字段

python - Web框架的局限性如何

python - 在 excel xlsxwriter python 中迭代多个 Dataframe 并写入 excel 电子表格

python - Flask - 'NoneType' 对象不可调用

javascript - 在页面中居中放置一个 div,但它出现在页面底部

html - 如何在 html 中自定义 onmouseover 属性?

html - 在CSS垂直菜单中添加间距

django i18n 在 mac 中不工作

python - 为什么相关矩阵的列小于 pandas Dataframe 的列

python - 带有python "-c"标志的linux命令管道