如何使用 jinja 将列添加到 table
How can I add a column to a table using jinja
首先让我告诉你,我是这方面的初学者,我正在做 CS50x 的最终项目。我的项目包含一个网页,该网页允许您将一些权重添加到数据库 table,然后它会显示这些权重并向您显示权重 gain/loss。我正在尝试使用 jinja(和 python)在 html 中呈现的 table 中显示查询结果。 RP 是标识符(您搜索 rp)。所需的输出是这样的:
[期望输出]
我的 python 代码如下:
@app.route("/weightquery", methods=["GET", "POST"])
@login_required
def weightquery():
if request.method == "POST":
weights = db.execute("SELECT rp, weight, date FROM weights WHERE rp=:rp AND sex=:sex AND user_id=:user_id ORDER BY date DESC",
rp=request.form.get("rp"), sex=request.form.get("sex"), user_id=session["user_id"])
gains = db.execute("SELECT weight FROM weights WHERE rp=:rp AND sex=:sex AND user_id=:user_id ORDER BY date DESC",
rp=request.form.get("rp"), sex=request.form.get("sex"), user_id=session["user_id"])
animal = request.form.get("rp")
for i in range(len(gains)):
for weight in gains[i]:
if i >= 0 and i < (len(gains)-1):
dif= gains[i][weight] - gains[i + 1][weight]
# Store the dif somewhere I can access.
gains[i].update({'weight': dif})
# Since the dif will always have one item less, I make sure to delete the last item.
gains[i].popitem()
return render_template("weightqueried.html", weights=weights, gains=gains, animal=animal, dif=dif)
else:
return render_template("weightquery.html")
我的 Html weightqueried.html 模板是:
{% block main %}
<div class="container">
<h3>{{ animal }}'s information</h3>
<table class="table table-striped">
<thead>
<tr>
<th>Date</th>
<th>Weight(kg)</th>
<th>Weight dif(kg)</th>
</tr>
</thead>
<tbody>
{% for rp in weights %}
<tr>
<td>{{ rp['date'] }}</td>
<td>{{ rp['weight'] }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
</tfoot>
</div>
{% endblock %}
非常感谢任何提示和指示,因为我正在努力学习,现在我的大脑被炸了!
如果完全删除 gains
,也许可以简化它。迭代 weights
,进行 dif
计算,并将结果添加到每个字典。然后在模板中,为 rp['dif']
添加一个
元素。如果我理解正确的话......
首先让我告诉你,我是这方面的初学者,我正在做 CS50x 的最终项目。我的项目包含一个网页,该网页允许您将一些权重添加到数据库 table,然后它会显示这些权重并向您显示权重 gain/loss。我正在尝试使用 jinja(和 python)在 html 中呈现的 table 中显示查询结果。 RP 是标识符(您搜索 rp)。所需的输出是这样的:
[期望输出]
我的 python 代码如下:
@app.route("/weightquery", methods=["GET", "POST"])
@login_required
def weightquery():
if request.method == "POST":
weights = db.execute("SELECT rp, weight, date FROM weights WHERE rp=:rp AND sex=:sex AND user_id=:user_id ORDER BY date DESC",
rp=request.form.get("rp"), sex=request.form.get("sex"), user_id=session["user_id"])
gains = db.execute("SELECT weight FROM weights WHERE rp=:rp AND sex=:sex AND user_id=:user_id ORDER BY date DESC",
rp=request.form.get("rp"), sex=request.form.get("sex"), user_id=session["user_id"])
animal = request.form.get("rp")
for i in range(len(gains)):
for weight in gains[i]:
if i >= 0 and i < (len(gains)-1):
dif= gains[i][weight] - gains[i + 1][weight]
# Store the dif somewhere I can access.
gains[i].update({'weight': dif})
# Since the dif will always have one item less, I make sure to delete the last item.
gains[i].popitem()
return render_template("weightqueried.html", weights=weights, gains=gains, animal=animal, dif=dif)
else:
return render_template("weightquery.html")
我的 Html weightqueried.html 模板是:
{% block main %}
<div class="container">
<h3>{{ animal }}'s information</h3>
<table class="table table-striped">
<thead>
<tr>
<th>Date</th>
<th>Weight(kg)</th>
<th>Weight dif(kg)</th>
</tr>
</thead>
<tbody>
{% for rp in weights %}
<tr>
<td>{{ rp['date'] }}</td>
<td>{{ rp['weight'] }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
</tfoot>
</div>
{% endblock %}
非常感谢任何提示和指示,因为我正在努力学习,现在我的大脑被炸了!
如果完全删除 gains
,也许可以简化它。迭代 weights
,进行 dif
计算,并将结果添加到每个字典。然后在模板中,为 rp['dif']
添加一个