英文:
send_filer return None or ended without a return statement
问题
I am trying to do an application where the user can download a plot.png and a file.csv, but send_files doesn't work. Here are my Python code:
app = Flask(__name__)
app.USE_X_SENDFILE = True
@app.route('/', methods=['POST', 'GET'])
def result():
if request.method == 'GET':
return render_template('result.html')
elif request.method == 'POST':
if request.form['submit_button'] == 'Download CSV':
return send_file(path_or_file='name.csv', as_attachment=True)
elif request.form['submit_button'] == 'Download plot':
return send_from_directory("static", "plot.png")
else:
return 'Error'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
And my HTML code:
{% extends "layout.html" %}
{% block content %}
<div class='wrap'>
<form method="POST">
<input type="submit" name="submit_button" value="Download CSV">
<input type="submit" name="submit_button" value="Download plot">
</form>
</div>
{% endblock %}
Both files are in the same directory as the server:
And the error is:
TypeError: The view function for 'result' did not return a valid response. The function either returned None or ended without a return statement.
I don't know why send_file returns None or without a statement. Thanks!!
英文:
I am trying to do a aplication where the user can dowload a plot.png and a file.csv but send_files doesnt work. Here are my python code:
app = Flask(__name__)
app.USE_X_SENDFILE = True
@app.route('/', methods= ['POST', 'GET'])
def result():
if request.method == 'GET':
return render_template('result.html')
elif request.method == 'POST':
if request.form['submit_button'] == 'Download CSV':
return send_file(path_or_file='name.csv', as_attachment=True, )
elif request.form['submit_button'] == 'Download plot':
return send_from_directory("static", "plot.png") #I tried with send_file and i had the same error
else:
return 'Error'
if __name__ == '__main__':
app.run(debug = True, host='0.0.0.0')
And my html code:
{% extends "layout.html" %}
{% block content %}
<div class = 'wrap'>
<form method="POST">
<input type="submit" name="submit_button" value="Download CSV">
<input type="submit" name="submit_button" value="Download plot">
</form>
</div>
{% endblock %}
Both file are in the same directory that the server:
And the error is:
TypeError: The view function for 'result' did not return a valid response. The function either returned None or ended without a return statement.
I dont know why send_file return None or without a statement. Thanks!!
答案1
得分: 2
如果request.form['submit_button']既不是'Download CSV'也不是'Download plot',那么result()将隐式返回None。只有当request.method既不是'POST'也不是'GET'时,才会返回'Error'。
英文:
If request.form['submit_button'] is neither 'Download CSV' nor 'Download plot' result() will implicitly return None. The return value of 'Error' will only happen if request.method is neither 'POST' nor 'GET'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论