一段代码
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+000:00应用[api]:生成成功
2020-05-07T00:37:39.445026+000:00 heroku[路由器]:at=info method=GET path="/"host=intelli-supermart.herokuapp.com request_id=bc70627f-fbff-4722-8b7e-97c18e7e2d5 fwd="203.128.16.105"dyno=web.1连接=1ms服务=102ms状态=500字节=470协议=https
2020-05-07T00:37:39.441994+00:0应用程序[web.1]:[20-05-07 00:37:39440]应用程序中的错误:/[GET]上出现异常
2020-05-07T00:37:39.442004+00:0应用[web.1]:追踪(最近一次通话(:
2020-05-07T00:37:39.442005+00:0 app[web.1]:文件"/app/.heroku/python/lib/python3.6/site packages/flack/app.py",第2447行,在wsgi_app 中
2020-05-07T00:37:39.442005+00:0 app[web.1]:响应=self.full_dispatch_request((
2020-05-07T00:37:39.442006+00:0 app[web.1]:文件"/app/.heroku/python/lib/python3.6/site-packages/flack/app.py",1952行,在full_dispatch_request 中
2020-05-07T00:37:39.442006+00:0 app[web.1]:rv=self.handle_user_exception(e(
2020-05-07T00:37:39.442007+00:0-app[web.1]:文件"/app/.heroku/python/lib/python3.6/site packages/flack/app.py",第1821行,在handle_user_exception 中
2020-05-07T00:37:39.442007+00:0 app[web.1]:重新评估(exc_type,exc_value,tb(
2020-05-07T00:37:39.442007+00:0-app[web.1]:文件"/app/.heroku/python/lib/python3.6/site packages/flak/_compat.py",第39行,重新评估
2020-05-07T00:37:39.442008+00:0 app[web.1]:提升价值
2020-05-07T00:37:39.442009+00:0-app[web.1]:文件"/app/.heroku/python/lib/python3.6/site packages/flack/app.py",第1950行,在full_dispatch_request 中
2020-05-07T00:37:39.442009+00:0 app[web.1]:rv=self.dispatch_request((
2020-05-07T00:37:39.442009+00:0-app[web.1]:文件"/app/.heroku/python/lib/python3.6/site packages/flack/app.py",第1936行,在dispatch_request 中
2020-05-07T00:37:39.442010+00:0 app[web.1]:返回self.view_functionsrule.endpoint
2020-05-07T00:37:39.442010+00:0-app[web.1]:文件"/app/app.py",第117行,在索引中
2020-05-07T00:37:39.442011+00:0应用[web.1]:wishlist_count=wishListCount((
2020-05-07T00:37:39.442011+00:0 app[web.1]:文件"/app/app.py",第79行,在wishListCount 中
2020-05-07T00:37:39.442011+00:0 app[web.1]:wishlist_count=len(会话['wishlist'](
2020-05-07T00:37:39.442012+00:0-app[web.1]:中的文件"/app/.heroku/python/lib/python3.6/site packages/werkzeug/local.py",第377行
2020-05-07T00:37:39.442012+00:0应用[web.1]:getitem=lambda x,i:x._get_current_object(([i]
2020-05-07T00:37:39.442012+00:0-app[web.1]:文件"/app/.heroku/python/lib/python3.6/site packages/flack/sessions.py",第84行,getitem
2020-05-07T00:37:39.442013+00:0 app[web.1]:返回super(SecureCookieSession,self(目标(密钥(
2020-05-07T00:37:39.442019+00:0应用[web.1]:KeyError:"Wishlist">
2020-05-07T00:37:39.445086+00:0 app[web.1]:10.11.150.203-[2020年5月7日0:00:37:39+0000]"GET/HTTP/1.1"500 290"-"Mozilla/5.0(X11;Linux x86_64(AppleWebKit/537.36(KHTML,类似Gecko(Chrome/80.4044.122 Safari/537.36">
键WishList
似乎不在session
变量中。你总是使用这个来检查会话中的密钥是否正确
if 'WishList' in session:
此外,您应该在代码的开头为session['WishList']
分配一些内容
您的会话中似乎没有Wishlist
。您可以尝试使用session.get('Wishlist')
获取变量,如果会话中没有Wishlist,则会返回None
。希望这会有所帮助。