Python 用flask求jinja2模板中的数和

Python 用flask求jinja2模板中的数和,python,python-2.7,flask,jinja2,Python,Python 2.7,Flask,Jinja2,我正在学习flask,并尝试创建一个web应用程序,该应用程序接受学生的姓名和分数,查找所有分数的总和,并在表中显示内容。但是总数总是显示为0 <form action = "/result" method = "POST"> <p>Name <input type = "text" name = "Name" /></p> <p>Physics <i

我正在学习flask,并尝试创建一个web应用程序,该应用程序接受学生的姓名和分数,查找所有分数的总和,并在表中显示内容。但是总数总是显示为
0

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>
代码如下所示

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>
mark_total.py:

from flask import Flask, render_template, request

app = Flask (__name__)

@app.route('/')
def student():
        return render_template('student.html')

@app.route('/result', methods = ['POST', 'GET'])
def result():
        if request.method == 'POST':
                result = request.form
                return render_template('result.html', result = result)

if __name__ == '__main__':
        app.run(host = '0.0.0.0', debug = True)
        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>
student.html:

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>

名字

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>
物理学

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>
化学

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>
数学

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>
result.html:

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>

结果
{{result[“Name”]}的结果
{%set总计=0%}
{result.iteritems()中的键为%val}
{%if键!='名称'%}
{%set-total=total | int+val | int%}
{{key}}
{{val}}
{{total}}
{%endif%}
{%endfor%}
全部的
{{total}}
输出的html源如下所示:

结果
ABC的结果
数学
100
100
化学
100
100
物理
100
100
全部的
0

问题在于,在循环内部定义的
总数与在循环外部定义(和访问)的
总数不同。这就是局部作用域所做的

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>
您可以通过使用这样的技巧(根据答案改编)来克服这一问题

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>

结果
{{result[“Name”]}的结果
{%set总计=[0]}
{result.iteritems()中的键为%val}
{%if键!='名称'%}
{%set}=total.append(total.pop()+val|int)%}
{{key}}
{{val}}
{{total[0]}
{%endif%}
{%endfor%}
全部的
{{total[0]}

但我真的不确定在模板中做这样的事情是否是个好主意。模板的主要思想是分离逻辑(如计算总数)和表示,这违反了这一原则。

当您更改循环内的变量值时,除dict外,不会影响循环外的变量值:

        <form action = "/result" method = "POST">
                <p>Name <input type = "text" name = "Name" /></p>
                <p>Physics <input type = "text" name = "Physics" /></p>
                <p>Chemistry <input type = "text" name = "chemistry" /></p>
                <p>Maths <input type ="text" name = "Mathematics" /></p>
                <p><input type = "submit" value = "submit" /></p>
        </form>

</body>
</html>
{% set total = {'value': 0} %}
...
    {% if total.update({"value": c|int + total.value|int }) %} {% endif %}
...
    {{total.value}}

您真的必须在模板中进行计算吗?为什么不在
mark_total.py
中计算此值并将结果传递给模板?是的。我尝试在python中进行更改,结果成功了。我想用模板试试。感谢Ilya不仅回答了这个问题,而且还提出了正确的方法。不仅仅是一个命令。实际上,任何可变对象都可以工作。