使用 Flask-SQLAlchemy 时遇到一个很奇怪的问题,使用下面的代码插入数据库会成功:
report = Report(tid=vulId, vulURL=vulURL, homeTitle=homeTitle, homeLink=homeLink, DBInfo=DBInfo,vulId=vulParId, UserLists=UserLists, dbsLists=dbsLists, content=content)
db.session.add(report)
奇怪的是 Mysql 中确实插入成功了,程序会跳转到 /report/view/vulId
return redirect("/report/view/"+str(vulId))
在 viewReport 函数中使用如下代码查询后返回 None :
report = Report.query.filter(Report.tid==vulId).first()
,但是此时如果在 model.py 的 main 函数中执行
report = Report.query.filter(Report.tid==vulId).first()
却会返回正确数据,此时再在 viewReport 函数中执行就不为空了。多次测试发现失败概率在 30%左右,现在不知道该怎么定位错误,麻烦各位指教!
相关代码
@app.route("/report/view/<int:vulId>", methods=["GET"])
return redirect("/vul/list")
def viewReport(vulId=-1):
if vulId < 0:
title = "Report-{}".format(vulId)
report = Report.query.filter(Report.tid==vulId).first()
if report == None:
reUrl = request.headers.get("Referer")
return redirect(reUrl)
return render_template("report.html", title=title, report=report)
@app.route("/report/make/<int:vulId>", methods=["GET", "POST"])
def makeReport(vulId=-1):
if vulId < 0:
return redirect("/vul/list")
form = ReportForm()
# print form.errors
if form.validate_on_submit():
# tid = form.tid.data
vulURL = form.vulURL.data
homeTitle = form.homeTitle.data
homeLink = form.homeLink.data
DBInfo = form.DBInfo.data
vulParId = form.vulId.data
UserLists = form.UserLists.data
dbsLists = form.dbsLists.data
content = form.content.data
report = Report(tid=vulId, vulURL=vulURL, homeTitle=homeTitle, homeLink=homeLink, DBInfo=DBInfo,
vulId=vulParId, UserLists=UserLists, dbsLists=dbsLists, content=content)
db.session.add(report)
vul = Task.query.filter(Task.tid==vulId).first()
vul.detectFlag = 2
db.session.commit()
return redirect("/report/view/"+str(vulId))
else:
title = "Report-{}".format(vulId)
vul = Task.query.filter(Task.tid==vulId).first()
vulTitle = ""
pageTitle = ""
try:
pageTitle = httpHandle.getTitle(vul.url)
except Exception, e:
pageTitle = "Get title error!{}".format(e)
encodeType = chardet.detect(pageTitle)
if encodeType["encoding"] == "GB2312":
vulTitle = pageTitle.decode("gb2312",'ignore')
else:
vulTitle = pageTitle.decode("utf-8",'ignore')
form.vulURL.process_data(vul.getURL())
form.content.process_data(vul.getRawData())
return render_template("makeReport.html", title=title, vul=vul, vulTitle=vulTitle, form=form)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.