来自 javascript 中编写的脚本的 jinja2 错误

jinja2 error from a script written in javascript

所以我有一个添加输入的按钮,在 python 的主要代码中我尝试接收请求表单,但 jinja 向我报错


jinja2.exceptions.TemplateSyntaxError

TemplateSyntaxError:需要标记 'end of print statement',得到 'string'


来自脚本的代码和 html:

<input type="button" onclick="addInput()" value='add'/>

<span id="responce"></span>
<script>
var countBox =1;
var boxName = 0;
function addInput()
{
    if (countBox<5) {
     var boxName="c"+countBox; 
     var k = "{{request.form.c" + countBox + "}}";
document.getElementById('responce').innerHTML+='<br/><input type="text" placeholder="choice'+countBox+'" name="choice'+countBox+'" value="'+k+'" />';
     countBox += 1;
}else{
    document.getElementById('responce').innerHTML+="<br><h5> You can't have more :)";
}
}
</script>

在主应用程序中:

def add():
    error = None
    if request.method == 'POST':
        c = []
        for i in range(1,4):
            h = 'c' + str(i)
            if request.form[h]!='':
                c.append(request.form[h])
     return redirect(url_for('questions'))

我不确定你想做什么,但这根本没有意义。 Jinja 模板在发送到客户端之前在服务器上进行评估;您不能使用 Javascript.

在浏览器中构建模板值

而且,似乎没有任何理由这样做。 Jinja 完全有能力做循环;您可以使用 Jinja 标签遍历列表,并从那里输出值。