我收到jinja2.exceptions.UndefinedError:登录到本地主机时'btn'是未定义的异常。 btn的定义正确,但仍然可以。我能否尽快获得帮助。先感谢您 Python代码-此代码为Python代码
from flask import Flask, render_template, request, send_file
from flask_sqlalchemy import SQLAlchemy
from send_email import send_email
from sqlalchemy.sql import func
from werkzeug import secure_filename
app=Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/success", methods=['POST'])
def success():
global file
if request.method=='POST':
file=request.files["file"]
file.save(secure_filename("uploaded" + file.filename))
with open("uploaded"+file.filename,"a") as f :
f.write("This was added later!")
return render_template("index.html", btn="download.html")
@app.route("/download")
def download():
return send_file("uploaded" + file.filename, attatchment_filename="yourfile.csv", as_attatchment=True)
if __name__ == '__main__':
app.debug=True
app.run()
HTML- 索引文件-这是主文件
<!DOCTYPE html>
<html lang="en">
<title> Data Collector App</title>
<head>
<link href="../static/main.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Collecting Height</h1>
<h3>Please Fill the Entries to get Population Statistics on Height</h3>
<div class="message">
{{text | safe}}
</div>
<form action="{{url_for('success')}}" method="POST" enctype="multipart/form-data">
<input type="file" name="file" > <br>
<button type="submit">Submit</button>
</form>
{%include btn ignore missing%}
</div>
</body>
</html>
下载文件-这是要下载的html文件。导入文件后,此html代码将在同一index.html页面中创建一个按钮,以便我也可以对同一文件进行下标
<!DOCTYPE html>
<html lang="en">
<div class="download">
<a href= {{url_for('download')}} target="blank"> <button class="btn"> Download </button></a>
</div>
</html>
答案 0 :(得分:0)
面临同样的问题。我的解决方法是替换:
{% include btn ignore missing %}
使用
{% if btn %}
{% include btn %}
{% endif %}