【问题标题】:flask hangs right before return jsonify ((no output error ))烧瓶在返回 jsonify 之前挂起((无输出错误))
【发布时间】:2018-11-10 01:24:31
【问题描述】:

我是一个新开发者,我创建了一个页面来检查用户是否已登录负载,如果他是,则将登录表单替换为来自服务器的其他数据

另一方面,如果不是,它会显示登录表单并等待用户单击登录以触发另一个 ajax 来验证和提取用户信息并返回 Json 对象。

问题发生在返回行之前 - 我追溯了打印和登录 js 的问题,在返回 jsonify 行之前一切正常

奇怪的是它从不引发任何错误,只是挂起

 def clogin():

print(f"\n login activated \n")
if request.method == "POST":
    loginName = request.form.get("memberNameid")
    loginPw = request.form.get("passwordloginid")
    #session['user'] = loginName


    f = Users.query.filter_by(email=loginName).first()
    login_user(f)
    session['user'] = f
    print(f"I have receved this from AJAX ... \n \n loginName = {loginName} \n loginPw = {loginPw} \n current_user = {current_user} \n current_user.id = {current_user.id} \n current_user.name = {current_user}")
    print(f"f.email = {f.email} \n f.pw = {f.password}")

    if f:
        qdisplayname = f.name
        print("qdisplayname = ",qdisplayname)
        if f.email == loginName: #and sha256_crypt.verify(loginPw,f.qpw):
            if f.pos == "pos2":
                print("pos : pos 2 was picked ")

                return jsonify({"Success": True , "Msg":" lets try to get jinja to work - email {{ current_user.email }} " , "position" : "Pharmacist" , "displayname":current_user.name })

这里是 CMD 输出:

登录激活

我从 AJAX 收到了这个...

loginName = qwe@qwe.qwe
loginPw = qwe
current_user = <Users 1>
current_user.id = 1
current_user.name = <Users 1>
f.email = qwe@qwe.qwe
f.pw = qwe
qdisplayname =  name2
pos : pos 2 was picked

如果您知道是什么让烧瓶像这样挂起,请告诉我? 我什至无法刷新或导航到其他页面我必须关闭服务器

【问题讨论】:

  • 最近也遇到了类似的问题...你知道当时是什么原因吗?
  • @MartinBucher 我刚刚为您回答了我自己的问题,当我检查旧文件时,我注意到我使用了flask_login,在我看来,处理登录请求是最可靠的。
  • 好吧,所以没有 jsonify({}) 了......我真的觉得这很奇怪。过去几天无法重现此问题,也许它已经修复了。但无论如何!

标签: json ajax python-3.x flash flask-login


【解决方案1】:

我最终转达 flask_login 来处理登录请求。

登录功能变成如下:

@app.route("/login", methods=['GET', 'POST'])
def login():

login_form = LoginForm()

# Allow login if validation success
if login_form.validate_on_submit():
    x = users.query.filter_by(username=login_form.username.data).first()
    login_user(x)
    db.session.commit()
    return redirect(url_for('index'))

return render_template("login.html", form=login_form)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2020-12-25
    相关资源
    最近更新 更多