请告诉我如何根据文件中的行数将其添加到模板中?
主要.py:
with open("ping.txt", "r") as file1:
notavailable = (file1.read())
print(notavailable)
app = Flask(__name__)
@app.route('/', methods=["POST", "GET"])
def index():
return render_template("index.html", noavailable=notavailable)
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=8080)
索引.py:
{% extends 'base.html' %}
{% block title %}
Главная страница
{% endblock %}
{% block body %}
<table class="table table-striped table-sm">
<thead>
<tr>
<th>Доступность комплекса: </th>
</tr>
</thead>
<tr>
<td>{{noavailable}}</td>
</tr>
</table>
{% endblock %}
notavailable在控制台中逐行显示,并且在页面上所有内容都包含在一行中(
你可以这样做:
main.py
索引.html
Ps
在将文件内容传递给 render_template() 和 index.html 中的逐行添加时添加了 .splitlines()。