使用烧瓶找到jinja2模板中的数字总和 [英] Finding the sum of numbers in jinja2 template using flask

查看:36
本文介绍了使用烧瓶找到jinja2模板中的数字总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习烧瓶,并试图创建一个接受学生姓名和标记的Web应用程序,查找所有标记的总和并在表格中显示内容.但是总和总是显示为0.

I am learning flask and tried to create a web app that accepts a student's name and marks, finds the total of all the marks and display things in a table. But the total always gets displayed as 0.

下面给出了代码

mark_total.py:

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)

student.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>

result.html:

result.html:

<html>
        <head>
                <title>Results</title>
        </head>

        <body>
                <h1>{{result["Name"]}}'s results</h1>
                {% set total = 0 %}
                <table border = 1>
                {% for key, val in result.iteritems() %}
                        {% if key != 'Name' %}
                                {% set total = total|int + val|int %}
                                <tr>
                                        <td>{{ key }}</td>
                                        <td>{{ val }}</td>
                                        <td>{{ total }}</td>
                                </tr>
                        {% endif %}
                {% endfor %}
                <tr>
                        <td>Total</td>
                        <td>{{total}}</td>
                </tr>
                </table>
        </body>
</html>

输出的html源如下:

The html source of the output is as follows:

<html>
	<head>
		<title>Results</title>
	</head>
	<body>
		<h1>ABC's results</h1>
		<table border = 1>
			<tr>
				<td>Mathematics</td>
				<td>100</td>
				<td>100</td>
			</tr>
			<tr>
				<td>chemistry</td>
				<td>100</td>
				<td>100</td>
			</tr>
			<tr>
				<td>Physics</td>
				<td>100</td>
				<td>100</td>
			</tr>
			<tr>
				<td>Total</td>
				<td>0</td>
			</tr>
		</table>
	</body>
</html>

有人可以帮助我了解如何解决此问题吗?

Can someone please help me understand how to solve this?

推荐答案

问题是您在循环内定义的total与在循环外定义(访问)的total不同.这是本地范围的作用.

The problem is that your total defined inside the loop is not the same total defined (and accessed) outside of loop. This is what local scopes do.

您可以使用类似的技巧(改编自答案)来解决此问题.

You can overcome this by using a hack like this (adapted from this answer).

<html>
        <head>
                <title>Results</title>
        </head>

        <body>
                <h1>{{result["Name"]}}'s results</h1>
                {% set total = [0] %}
                <table border = 1>
                {% for key, val in result.iteritems() %}
                        {% if key != 'Name' %}
                                {% set _ = total.append(total.pop() + val|int) %}
                                <tr>
                                        <td>{{ key }}</td>
                                        <td>{{ val }}</td>
                                        <td>{{ total[0] }}</td>
                                </tr>
                        {% endif %}
                {% endfor %}
                <tr>
                        <td>Total</td>
                        <td>{{ total[0] }}</td>
                </tr>
                </table>
        </body>
</html>

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

But I'm really not sure that it is a good idea to do such things in a template. The main idea of templates is to separate the logic (like counting the totals) and the representation, and this is a violation of this principle.

这篇关于使用烧瓶找到jinja2模板中的数字总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆