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

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

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:

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:

app = Flask(__name__)
app.USE_X_SENDFILE = True

@app.route(&#39;/&#39;, methods= [&#39;POST&#39;, &#39;GET&#39;])
def result():
    if request.method == &#39;GET&#39;:
        return render_template(&#39;result.html&#39;)
    elif request.method == &#39;POST&#39;:
             
            if request.form[&#39;submit_button&#39;] == &#39;Download CSV&#39;:
                return send_file(path_or_file=&#39;name.csv&#39;, as_attachment=True, )
                

            elif request.form[&#39;submit_button&#39;] == &#39;Download plot&#39;:
                return send_from_directory(&quot;static&quot;, &quot;plot.png&quot;) #I tried with send_file and i had the same error
    else:
         return &#39;Error&#39;       
    

if __name__ == &#39;__main__&#39;:
    app.run(debug = True, host=&#39;0.0.0.0&#39;)  

And my html code:

{% extends &quot;layout.html&quot; %}


{% block content %}

&lt;div class = &#39;wrap&#39;&gt;

    &lt;form method=&quot;POST&quot;&gt;

        &lt;input type=&quot;submit&quot; name=&quot;submit_button&quot; value=&quot;Download CSV&quot;&gt;
        &lt;input type=&quot;submit&quot; name=&quot;submit_button&quot; value=&quot;Download plot&quot;&gt;

    &lt;/form&gt;
&lt;/div&gt;

{% 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:

确定