我正在开发一个使用python生成条形码图像的网站。我正在执行以下操作:
我使用的所有模块和软件都是最新的。我正在使用Visual Studio在本地计算机上测试代码。当我部署到heroku时,尝试执行任何操作(单击按钮等)时,会看到内部错误。
这是我的main.py
文件:
import sys
import shutil
from flask import Flask, render_template, request, redirect
from flask import *
from flask import send_file, send_from_directory, safe_join, abort
from copy import copy
from main import app as Application
import barcode
from barcode.writer import ImageWriter
from barcode.codex import Code39
import os
app = Flask(__name__, template_folder='templates')
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.after_request
def add_header(response):
response.cache_control.max_age = 100
return response
@app.route('/', methods=['GET'])
def root():
path = os.path.join(app.root_path, 'static', 'image.png')
delete_path = os.path.join(app.root_path, "static")
error = None
for i in os.listdir(delete_path):
if i.startswith('image'): # not to remove other images
os.remove(path)
return render_template("index.html", error=error)
def is_nric_valid(nric):
nricCheckDigits = 'JZIHGFEDCBA'
finCheckDigits = 'XWUTRQPNMLK'
weights = [2, 7, 6, 5, 4, 3, 2]
if len(nric) != 9:
return False
firstChar = nric[0].upper()
digits = nric[1:8]
lastChar = nric[8].upper()
if firstChar not in "STFG" or not digits.isdigit():
return False
total = 0
if firstChar in "GT":
total += 4
for i in range(7):
total += int(digits[i]) * weights[i]
if firstChar in "ST":
return nricCheckDigits[total % 11] == lastChar
return finCheckDigits[total % 11] == lastChar
@app.route('/generate', methods=['GET'])
def generate():
if "nric" in request.args:
nric = request.args.get('nric')
nric = str(nric)
path = os.path.join(app.root_path, 'static', 'image.png')
delete_path = os.path.join(app.root_path, "static")
filename = "image"
for i in os.listdir(delete_path):
if i.startswith('image'): # not to remove other images
os.remove(path)
if nric == '':
error = '<Empty Input>'
return render_template("index.html", error=error)
elif is_nric_valid(nric) == True:
code39 = Code39(nric, add_checksum = False, writer=barcode.writer.ImageWriter())
print(code39)
print(type(code39))
print(Code39)
print(type(Code39))
os.chdir(delete_path)
image = code39.save(filename)
# a = os.path.abspath(image)
# b = delete_path
# shutil.move(a,b)
return render_template("barcode.html", nric=nric)
else:
error = '<Invalid NRIC>'
check = True
return render_template("index.html", error=error)
else:
error = '<Empty Input>'
return render_template("index.html", error=error)
if __name__ == '__main__':
app.run(debug=False, use_reloader=True)
这是heroku日志中的错误:
2020-10-05T22:21:40.260058+00:00 app[web.1]: File
"/app/.heroku/python/lib/python3.8/site-packages/flask/app.py", line
1950, in full_dispatch_request 2020-10-05T22:21:40.260058+00:00
app[web.1]: rv = self.dispatch_request()
2020-10-05T22:21:40.260059+00:00 app[web.1]: File
"/app/.heroku/python/lib/python3.8/site-packages/flask/app.py", line
1936, in dispatch_request 2020-10-05T22:21:40.260060+00:00 app[web.1]:
return self.view_functions[rule.endpoint](**req.view_args)
2020-10-05T22:21:40.260060+00:00 app[web.1]: File "/app/main.py",
line 88, in generate 2020-10-05T22:21:40.260061+00:00 app[web.1]:
code39 = Code39(nric, add_checksum = False,
writer=barcode.writer.ImageWriter()) 2020-10-05T22:21:40.260061+00:00
app[web.1]: TypeError: 'NoneType' object is not callable
2020-10-05T22:21:40.267816+00:00 app[web.1]: 10.30.127.22 - -
[05/Oct/2020:22:21:40 +0000] "GET /generate?nric=T0325317H HTTP/1.1"
500 290 "https://sg-nric2020.herokuapp.com/" "Mozilla/5.0 (Windows NT
10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" 2020-10-05T22:21:40.268527+00:00
heroku[router]: at=info method=GET path="/generate?nric=T0325317H"
host=sg-nric2020.herokuapp.com
request_id=d059d16b-d2e1-416a-a4e5-621263f17aac fwd="116.87.35.254"
dyno=web.1 connect=0ms service=19ms status=500 bytes=534
protocol=https 2020-10-05T22:54:17.303099+00:00 heroku[router]:
at=info method=GET path="/" host=sg-nric2020.herokuapp.com
request_id=7010e349-701f-42ea-9669-a32dee985a87 fwd="149.154.161.7"
dyno=web.1 connect=0ms service=64ms status=200 bytes=2655
protocol=https
我知道此应用程序可能不需要导入的模块,但我将其留在那里,因为我正试图解决此问题。我浏览了StackOverflow以获得答案,并仔细研究了这个问题,得出的结论是,Heroku中的gunicorn与我在烧瓶中的应用程序交谈时出现了问题。但是我无法真正解决它。因此,这是我在Procfile
中的代码:
web: gunicorn main:app --preload
如果有人面临与我的问题类似的事情,我希望能听到一些回应。我之前已经使用Heroku部署了2个其他Web应用程序,并且都运行良好。谢谢您的宝贵时间!