作为一名新手,天天都踩坑,坑坑不一样。
又有新状况出现,flask开发web网站,在app中储存了session供页面调取,但是为毛相同的模型结构,相同的调用方法,有的页面正常,有的页面session.get()调取的却是None呢?百思不得其解,代码都翻烂了也没找到原因。
先记在这里,等我成为高手,再来嘲笑一下自己。
记录session
if username != None and password != None: user = Users() result = user.find_by_username(username) if len(result) == 1 and result[0].password == password: session['islogin'] = 'true' session['userid'] = result[0].id session['username'] = username session['nickname'] = result[0].nickname session['role'] = result[0].role session['avatar'] = result[0].avatar
调取为None的代码:
from flask import Blueprint, request from module.article import Article from module.answer import Answer from module.credit import Credit from module.users import Users answer = Blueprint('answer', __name__) @answer.route('/answer', methods=['POST']) def add(): articleid = request.form.get('articleid') content = request.form.get('content').strip() ipaddr = request.remote_addr # 对回答内容进行校验 if len(content) < 5 or len(content) > 10000: return 'content-invalid' answer = Answer() if not answer.check_limit_per_10(): answer.insert_answer(articleid, content, ipaddr) Credit().insert_detail(type='添加评论', target=articleid, credit=2) Users().update_credit(2) Article().update_replycount(articleid) return 'add-pass' else: return 'add-limit'
已解决!通过前端页面带参数请求,后端再写了专门的函数来处理。
但是治标不治本,此页面中session.get()未能取值的原因依然是个谜~
如有高手,欢迎探讨~