烧瓶:jQuery 内的 Jinja

Flask: Jinja inside of jQuery

我正在通过 flask 制作一个网络应用程序,我正在使用 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() 时不需要重复使用 {{ }} 符号,因为它是 shorthand 将表达式打印到模板输出。

正确的用法是 {{ url_for('thread_status', jobid=jobid) }}