(500内部服务器错误])烧瓶发布请求导致服务器错误[烧瓶]

时间:2019-06-27 04:19:40

标签: python html heroku flask

我已经在Heroku上部署了一个简单的flask应用程序(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"

但是我确实有一个return语句,如您在我的python代码中所见。任何帮助将不胜感激!!!

1 个答案:

答案 0 :(得分: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')