send_filer 返回 None 或在没有返回语句的情况下结束

huangapple go评论112阅读模式
英文:

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:

  1. app = Flask(__name__)
  2. app.USE_X_SENDFILE = True
  3. @app.route('/', methods=['POST', 'GET'])
  4. def result():
  5. if request.method == 'GET':
  6. return render_template('result.html')
  7. elif request.method == 'POST':
  8. if request.form['submit_button'] == 'Download CSV':
  9. return send_file(path_or_file='name.csv', as_attachment=True)
  10. elif request.form['submit_button'] == 'Download plot':
  11. return send_from_directory("static", "plot.png")
  12. else:
  13. return 'Error'
  14. if __name__ == '__main__':
  15. app.run(debug=True, host='0.0.0.0')

And my HTML code:

  1. {% extends "layout.html" %}
  2. {% block content %}
  3. <div class='wrap'>
  4. <form method="POST">
  5. <input type="submit" name="submit_button" value="Download CSV">
  6. <input type="submit" name="submit_button" value="Download plot">
  7. </form>
  8. </div>
  9. {% endblock %}

Both files are in the same directory as the server:

send_filer 返回 None 或在没有返回语句的情况下结束

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!! send_filer 返回 None 或在没有返回语句的情况下结束

英文:

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:

  1. app = Flask(__name__)
  2. app.USE_X_SENDFILE = True
  3. @app.route(&#39;/&#39;, methods= [&#39;POST&#39;, &#39;GET&#39;])
  4. def result():
  5. if request.method == &#39;GET&#39;:
  6. return render_template(&#39;result.html&#39;)
  7. elif request.method == &#39;POST&#39;:
  8. if request.form[&#39;submit_button&#39;] == &#39;Download CSV&#39;:
  9. return send_file(path_or_file=&#39;name.csv&#39;, as_attachment=True, )
  10. elif request.form[&#39;submit_button&#39;] == &#39;Download plot&#39;:
  11. return send_from_directory(&quot;static&quot;, &quot;plot.png&quot;) #I tried with send_file and i had the same error
  12. else:
  13. return &#39;Error&#39;
  14. if __name__ == &#39;__main__&#39;:
  15. app.run(debug = True, host=&#39;0.0.0.0&#39;)

And my html code:

  1. {% extends &quot;layout.html&quot; %}
  2. {% block content %}
  3. &lt;div class = &#39;wrap&#39;&gt;
  4. &lt;form method=&quot;POST&quot;&gt;
  5. &lt;input type=&quot;submit&quot; name=&quot;submit_button&quot; value=&quot;Download CSV&quot;&gt;
  6. &lt;input type=&quot;submit&quot; name=&quot;submit_button&quot; value=&quot;Download plot&quot;&gt;
  7. &lt;/form&gt;
  8. &lt;/div&gt;
  9. {% endblock %}

Both file are in the same directory that the server:

send_filer 返回 None 或在没有返回语句的情况下结束

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!! send_filer 返回 None 或在没有返回语句的情况下结束

答案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'

huangapple
  • 本文由 发表于 2023年6月15日 00:30:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定