【发布时间】:2021-12-10 11:18:47
【问题描述】:
我正在尝试使用 Flask 和 Jinja2 在我的 Web 应用程序中显示来自 CSV 文件的数据。当我激活我的代码时,我的印象是 Flaks 无法链接保存在子文件夹“模板”中的 HTML 代码和我的 python 代码(app.py)和保存在主文件“项目”中的 csv 文件。
你能帮帮我吗?请在下面找到我的代码。
Python 代码(app.py):
import flask
import jinja2
from flask import Flask
from flask import render_template
from flask import request
import csv
@app.route("/seeall")
def seeallform():
html_file = open("templates/seeall.html")
content = html_file.read()
html_file.close()
return content
``def template2():
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
TEMPLATE_FILE = "templates/seeall.html"
template = templateEnv.get_template(TEMPLATE_FILE)
outputText = template.render()
print (outputText)
def table_allissues():
if request.method == "GET":
return render_template ("templates/seeall.html")
elif request.method == "POST":
results = []
data_csv = request.form.get("data.csv").split('\n')
reader = csv.DictReader(data_csv)
for row in reader:
results.append(dict(row))
return render_template("templates/seeall.html", results=results)
HTML 代码(seeall.htlm):
<!DOCTYPE html>
<html lang="en">
<head>
<title>View all issues</title>
</head>
<body>
<header>
<p><a href="/">Go back on homepage</a></p>
</header>
<table>
<tr>
{% for headers in results[0].keys() %}
<th>{{header}}</th>
{% endfor %}
</tr>
{% for row in results %}
<tr>
<td>{{row['Regulatory_Domain']}}</td>
<td>{{row['Detection_Date']}}</td>
<td>{{row['ID_Client']}}</td>
<td>{{row['Issue_ID_Name']}}</td>
<td>{{row['Status_Issue']}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
【问题讨论】:
标签: python csv flask html-table jinja2