尝试在Heroku上部署Flask应用程序,部署工作正常,直到我在代码中添加会话,然后出现500个内部错误。该如何解决?

问题描述 投票:0回答:2

代码段

def wishListCount():
wishlist_count = len(session['Wishlist'])
if len(session['Wishlist']) <= 0:
    return 0    
else:
    return wishlist_count

@app.route('/wishlist', methods=['GET', 'POST', 'DELETE'])
def wishlist():
if request.method == 'POST':
    product_id = int(request.form['product_id'])
    ListItems = [product_id]
    if 'Wishlist' in session:
        if product_id in session['Wishlist']:
            print("This product is already in wishList!")
        else:
            session['Wishlist'] = mergeDict(session['Wishlist'], ListItems)
    else:
        session['Wishlist'] = ListItems
wishlist_count = wishListCount()

Heroku日志

状态从启动更改为启动

2020-05-07T00:37:33.000000 + 00:00 app [api]:构建成功

2020-05-07T00:37:39.445026 + 00:00 heroku [router]:at = info method = GET path =“ /” host = intelli-supermart.herokuapp.com request_id = bc70627f-fbff-4722-8b7e- f97c18e7e2d5 fwd =“ 203.128.16.105” dyno = web.1 connect = 1ms服务= 102ms状态= 500字节= 470协议= https

2020-05-07T00:37:39.441994 + 00:00应用程序[web.1]:[2020-05-07 00:37:39,440]应用程序中出现错误:/ [GET]上的异常

2020-05-07T00:37:39.442004 + 00:00 app [web.1]:回溯(最近一次通话过去):

2020-05-07T00:37:39.442005 + 00:00 app [web.1]:文件“ /app/.heroku/python/lib/python3.6/sitepackage/flask/app.py”,第2447行,在wsgi_app中]]

2020-05-07T00:37:39.442005 + 00:00 app [web.1]:响应= self.full_dispatch_request()

2020-05-07T00:37:39.442006 + 00:00 app [web.1]:文件“ /app/.heroku/python/lib/python3.6/site-包/烧瓶/app.py”,行1952,在full_dispatch_request中

2020-05-07T00:37:39.442006 + 00:00 app [web.1]:rv = self.handle_user_exception(e)

2020-05-07T00:37:39.442007 + 00:00 app [web.1]:文件“ /app/.heroku/python/lib/python3.6/site-packages/flask/app.py”,行1821,在handle_user_exception中

2020-05-07T00:37:39.442007 + 00:00 app [web.1]:重新提高(exc_type,exc_value,tb)

2020-05-07T00:37:39.442007 + 00:00 app [web.1]:文件“ /app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py”,行39,在加价中

2020-05-07T00:37:39.442008 + 00:00 app [web.1]:提高价值

2020-05-07T00:37:39.442009 + 00:00 app [web.1]:文件“ /app/.heroku/python/lib/python3.6/site-packages/flask/app.py”,行1950年,在full_dispatch_request

2020-05-07T00:37:39.442009 + 00:00 app [web.1]:rv = self.dispatch_request()

2020-05-07T00:37:39.442009 + 00:00 app [web.1]:文件“ /app/.heroku/python/lib/python3.6/site-packages/flask/app.py”,行1936,在dispatch_request中

2020-05-07T00:37:39.442010 + 00:00 app [web.1]:返回self.view_functionsrule.endpoint

2020-05-07T00:37:39.442010 + 00:00 app [web.1]:文件“ /app/app.py”,索引中的第117行

2020-05-07T00:37:39.442011 + 00:00 app [web.1]:wishlist_count = wishListCount()

2020-05-07T00:37:39.442011 + 00:00 app [web.1]:文件“ /app/app.py",wishListCount中的第79行

2020-05-07T00:37:39.442011 + 00:00 app [web.1]:wishlist_count = len(session ['Wishlist'])]

2020-05-07T00:37:39.442012 + 00:00 app [web.1]:文件“ /app/.heroku/python/lib/python3.6/site-packages/werkzeug/local.py”,行377,在

2020-05-07T00:37:39.442012 + 00:00 app [web.1]:getitem

= lambda x,i:x._get_current_object()[i]

2020-05-07T00:37:39.442012 + 00:00 app [web.1]:文件“ /app/.heroku/python/lib/python3.6/site-packages/flask/sessions.py”,行84,在getitem

2020-05-07T00:37:39.442013 + 00:00 app [web.1]:返回超级(SecureCookieSession,self)。getitem

(key)

2020-05-07T00:37:39.442019 + 00:00 app [web.1]:KeyError:'Wishlist'

2020-05-07T00:37:39.445086 + 00:00 app [web.1]:10.11.150.203--[07 / May / 2020:00:37:39 +0000]“ GET / HTTP / 1.1” 500 290“-”“ Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 81.0.4044.122 Safari / 537.36”

代码段的wishListCount():wishlist_count = len(session ['Wishlist'])如果len(session ['Wishlist'])<= 0:返回0否则:返回wishlist_count @ app.route('/ wishlist' ,方法= ['...

python flask heroku web-applications web-deployment
2个回答
0
投票

似乎键WishList不在session变量中。始终使用此命令检查会话中的密钥]


0
投票

似乎您的会话中没有Wishlist。您可以尝试使用session.get('Wishlist')获取变量,如果会话中没有心愿单,则将返回None。希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.