gpt4 book ai didi

flask - 将javascript变量传递到jinja2中的宏中

转载 作者:行者123 更新时间:2023-12-03 17:34:31 30 4
gpt4 key购买 nike

我尝试在$.get()失败时显示简单的错误消息

$.get('{{ url_for(c) }}', function(data) {
$('#{{ chart_id }}').highcharts(data);
}).fail(function(resp) {
{# I am not sure why this is not working as intended #}
{% set error_html = error(resp, c)|replace('\n', '\\\n') %}
$('#{{ chart_id }}').html('{{ error_html }}');
});


但是,似乎无法访问我的 resp对象,它是一个javascript变量。如何将其传递给宏,就像字典一样?

最佳答案

问题是Jinja在页面加载之前运行:

{# what Jinja sees #}
text ... {{ url_for(c) }} ... text ...
text ... {{ chart_id }} ... text ...
... text ...
{# I am not sure why this is not working as intended #}
{% set error_html = error(resp, c)|replace('\n', '\\\n') %}
... text ... {{ chart_id }} ... text ... {{ error_html }} ... text ...
{# What on earth is `resp`? #}


您实际上并不想在模板级别上编写它,而是采取响应并在JavaScript中使用它:

.fail(function(resp) {
var displayError = calculateDisplayErrorFromResponse(resp);
$('#{{ chart_id }}').html(displayError);
});

关于flask - 将javascript变量传递到jinja2中的宏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24123618/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com