Python Sijax和flaskurl重定向

Python Sijax和flaskurl重定向,python,flask,sijax,Python,Flask,Sijax,我正在尝试使用一点ajax制作一个登录表单。当用户填写错误的密码/用户名组合时,错误消息将被添加到带有sijax的页面: 以下是我的两种方法: 1) Sijax方法 @staticmethod def login(obj_response, uname, password): # Verify the user. username = uname.strip() password = password.strip() user = User.query.filte

我正在尝试使用一点ajax制作一个登录表单。当用户填写错误的密码/用户名组合时,错误消息将被添加到带有sijax的页面:

以下是我的两种方法:
1) Sijax方法

@staticmethod
def login(obj_response, uname, password):
    # Verify the user.
    username = uname.strip()
    password = password.strip()
    user = User.query.filter_by(username = username).first()

    if user is None:
        error = 'Invalid username/password combination'
    elif password != user.password:
        error = 'Invalid username/password combination'

    # Log the user in if the info is correct.
    else:
        login_user(user)
        session['logged_in'] = True 
        obj_response.redirect(url_for('user_home'))

    # Clear the previous error message.
    obj_response.script("$('#errormessage').remove();")

    # Add an error message to the html if there is an error.
    obj_response.html_append(".loginform", "<h4 id='errormessage'>" + error + "</h4>") 
我想做的是检查用户名/密码组合是否正确,是否没有使用ajax显示错误消息,如果正确,则将用户重定向到其主页(url_代表('userhome'))

我正在尝试使用sijax方法:obj_response.redirect(url_for('user_home')),但这不起作用

有什么想法吗

我得到这个错误: obj_response.html_append(“.loginform”,”+错误+”)
UnboundLocalError:赋值前引用的局部变量“error”

问题是您总是使用
error
,但仅在发生错误时定义它

简单的解决方案是:在
之前添加
error=None
,如果用户为None:
行。除此之外,仅在出现错误时创建错误消息元素:

if error:
    obj_response.html_append(".loginform", "<h4 id='errormessage'>" + error + "</h4>") 
如果出现错误:
obj_response.html_append(“.loginform”,”+错误+”)
if error:
    obj_response.html_append(".loginform", "<h4 id='errormessage'>" + error + "</h4>")