【问题标题】:(500 Internal Server Error])Flask Post request is causing the server Error [Flask](500 Internal Server Error])Flask Post 请求导致服务器错误 [Flask]
【发布时间】:2019-06-27 04:19:40
【问题描述】:

我在 Heroku 上部署了一个简单的烧瓶应用程序 (https://calc-cogs.herokuapp.com/),但它给了我以下错误

Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

当我在没有任何文件的情况下提交时,它给了我这个错误,但在我提交文件时没有给我这个错误。但问题是我在这两种情况下都返回了类似的东西。

我的 python 代码在下面,这给了我错误。

@app.route('/', methods = ['GET', 'POST'])
def upload_file_calculate():
   if request.method == 'POST':
      if ('goodsold' not in request.files) or ('costingsheet' not in request.files) :
         return render_template('upload.html')
      good_sold = request.files['goodsold']
      costing_sheet = request.files['costingsheet']
      if good_sold and costing_sheet:
         calc=calc_cogs(good_sold,costing_sheet)
         return render_template('upload.html',COGS=calc.calculate()[0],Profit=calc.calculate()[1],items=calc.calculate()[2])
   else:
      return render_template('upload.html')

这里是我提交帖子请求的 HTML 代码

<form action = "{{ url_for('upload_file_calculate') }}" method = "POST" 
                    enctype = "multipart/form-data">
                        <div class="form-group">
                            <h4 align="center" class="card-title">Goods Sold</h4>
                            <input type="file" class="form-control-file" name="goodsold">
                        </div>
                        <div class="form-group">
                            <h4 align="center" class="card-title">Costing Sheet</h4>
                            <input type="file" class="form-control-file" name="costingsheet">
                        </div>
                        <div class="card-footer bg-primary">
                            <button align="center" type="submit" class="btn btn-block bg-white font-weight-bold">Submit</button>
                        </div>
                    </form>

追踪后出现如下错误

2019-06-27T04:08:14.448235+00:00 app[web.1]: [2019-06-27 04:08:14,447] ERROR in app: Exception on / [POST]
2019-06-27T04:08:14.448260+00:00 app[web.1]: Traceback (most recent call last):
2019-06-27T04:08:14.448263+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2311, in wsgi_app
2019-06-27T04:08:14.448265+00:00 app[web.1]: response = self.full_dispatch_request()
2019-06-27T04:08:14.448268+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1835, in full_dispatch_request
2019-06-27T04:08:14.448271+00:00 app[web.1]: return self.finalize_request(rv)
2019-06-27T04:08:14.448273+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1850, in finalize_request
2019-06-27T04:08:14.448275+00:00 app[web.1]: response = self.make_response(rv)
2019-06-27T04:08:14.448277+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1976, in make_response
2019-06-27T04:08:14.448280+00:00 app[web.1]: 'The view function did not return a valid response. The'
2019-06-27T04:08:14.448291+00:00 app[web.1]: TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
2019-06-27T04:08:14.449117+00:00 app[web.1]: 10.164.179.60 - - [27/Jun/2019:04:08:14 +0000] "POST / HTTP/1.1" 500 290 "https://calc-cogs.herokuapp.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"

但正如您在我的 python 代码中看到的那样,我确实有一个 return 语句。任何帮助将不胜感激!!!

【问题讨论】:

  • good_sold and costing_sheet条件失败时没有return语句
  • if request.method == 'POST': 内你有两个if 但你没有else: return render_template(..) 或至少return render_template(..) 所以它可能默认运行return None
  • @furas 太棒了!请将您的评论作为答案,以便我接受!

标签: python html heroku flask


【解决方案1】:

if request.method == 'POST': 中,你有两个if,但你没有

else: 
    return render_template(..) 

或至少

return render_template(..) 

所以它可以默认运行return None

@app.route('/', methods = ['GET', 'POST'])
def upload_file_calculate():
   if request.method == 'POST':
      if ('goodsold' not in request.files) or ('costingsheet' not in request.files) :
         return render_template('upload.html')
      good_sold = request.files['goodsold']
      costing_sheet = request.files['costingsheet']
      if good_sold and costing_sheet:
         calc=calc_cogs(good_sold,costing_sheet)
         return render_template('upload.html',COGS=calc.calculate()[0],Profit=calc.calculate()[1],items=calc.calculate()[2])

      return render_template(...)  # <--- need it instead of default `return None`

   else:
      return render_template('upload.html')

也许你可以用不同的方式写它——使用and而不是or/not——最后只有一个return render_template('upload.html')

@app.route('/', methods = ['GET', 'POST'])
def upload_file_calculate():
   if request.method == 'POST':
      if ('goodsold' in request.files) and ('costingsheet' in request.files) :
          good_sold = request.files['goodsold']
          costing_sheet = request.files['costingsheet']
          if good_sold and costing_sheet:
             calc = calc_cogs(good_sold,costing_sheet)
             return render_template('upload.html', COGS=calc.calculate()[0], Profit=calc.calculate()[1], items=calc.calculate()[2])

   return render_template('upload.html')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    相关资源
    最近更新 更多