烧瓶:jQuery内部的Jinja



我正在通过flask制作一个web应用程序,并使用jQuery在线程完成后呈现不同的模板。模板的路由是可变的,我想在我的jQuery(Ajax(脚本中用jinja指定这一点。我现在正在使用以下代码,这导致了以下错误:

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) }}

最新更新