简体   繁体   中英

(500 Internal Server Error])Flask Post request is causing the server Error [Flask]

I have deployed a simple flask app ( https://calc-cogs.herokuapp.com/ ) on Heroku but it is giving me the following error

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.

It gives me this error when I submit without any files but doesn't give me this error when I submit the files. But the thing is that I am returning similar things in both of those situations.

My python code is below which is giving me the error.

@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 code where I am submitting a post request is here

 <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> 

I got the following error after tracing

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"

But I did have a return statement as you can see in my python code. Any help would be appreciated!!!

Inside if request.method == 'POST': you have two if but you don't have

else: 
    return render_template(..) 

or at least

return render_template(..) 

so it may runs return None as default

@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')

Probably you could write it in different way - using and instead of or/not - to have only one return render_template('upload.html') at the end.

@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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM