python - Flask:jQuery 中的 Jinja
问题描述
我正在通过烧瓶制作一个网络应用程序,并且我正在使用 jQuery 以便在线程完成后呈现不同的模板。模板的路径是可变的,我想用 jinja 在我的 jQuery (Ajax) 脚本中指定它。我现在正在使用以下代码,这会导致以下错误:
HTML:
{% block script %}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" lang="javascript">
$(document).ready(function () {
var refresh_id = setInterval(function () {
$.get(
"{{ url_for('thread_status', jobid={{ jobid }}) }}",
function (data) {
console.log(data);
if (data.status == 'finished') {
if (data.success == 'success'){
window.location.replace("{{ url_for('result' , jobid={{ jobid }}) }}");
}
else if (data.succes == "failed"){
window.location.replace("{{ url_for('typing_failed') }}");
}
}
}
)
}
, 5000); // refresh every 5 seconds
});
</script>
{% endblock %}
Python:
@app.route('/<jobid>/results/')
def result(jobid):
return render_template("results.html")
@app.route('/<jobid>/status')
def thread_status(jobid):
global th
return jsonify(dict(status=('running' if th.is_alive() else 'finished'), success=success))
错误:
line 8, in template
"{{ url_for('thread_status', jobid={{ jobid }}) }}",
jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'
解决方案
{{ }}
调用时无需重用符号,url_for()
因为它是将表达式打印到模板输出的简写。
正确的用法是{{ url_for('thread_status', jobid=jobid) }}
.
推荐阅读
- javascript - 如何在所有设备上全屏制作乐天动画?
- python - 我的数独查找器有什么问题?
- reactjs - ReactJS input type = "text" maxLength 在作为道具传递时不起作用
- java - 用于 Hadoop 的 Teradata 连接器因超出范围异常而失败
- ios - 如何在 iOS 13 或更高版本中使用 Sinch VOIP?
- reactjs - 如何使用 React Apollo Link Rest 获取错误正文
- airflow - 在每月的 12 号和每月最后一天的前 2 天在 Airflow 上执行任务
- c# - 将新列添加到现有列表
- r - 将 data.table 的列转换为同一 data.table 中的列表
- javascript - 如何在没有 JSX 的元素中添加自定义属性(仅使用 createElement 方法)?