【问题标题】:Url in browser not updated after call of redirect( url_for('xxx' )) in Flask with jQuery mobile使用 jQuery mobile 在 Flask 中调用重定向(url_for('xxx'))后浏览器中的 URL 未更新
【发布时间】:2012-12-01 19:47:24
【问题描述】:

我有一个使用 Flask 的非常简单的 python 程序,如下所示。它通过弹出和注销处理登录。问题是浏览器中的 url 没有被 redirect(url_for()) 调用更新。

@app.route('/')
def index():
    if not 'username' in session:
        # contains a button showing a login popup form with action set to '/login'
        return render_template('welcome.html')
    else:
        # contains a logout button with a href to '/logout'
        return render_template('webapp.html') 


@app.route('/login', methods=['POST'])
def login():
    session['username'] = request.form['username']
    return redirect(url_for('index'))


@app.route('/logout')
def logout():
    session.pop('username', None)
    return redirect(url_for('index'))

访问“/”时,会显示欢迎页面。当我单击该按钮时,会显示登录弹出窗口,并且其表单操作会重定向到“/login”。这有效,并且 login() 函数被调用并执行。重定向也是如此,但浏览器不会更新显示的 url。

所以 webapp 页面显示为 /logon url。当我单击重新加载时,我收到一个错误,因为它尝试重新加载 /logon,而它应该重新加载已重定向的“/”。

/logout 也是如此。当显示 webapp 页面并单击注销按钮时,将加载 /logout 页面,该页面执行 logout() 函数并重定向到 index.html。但是该网址留待注销。

如果我然后重新加载页面,它会成功,因为 /logout 接受 GET 方法,然后 url 会更新为 / ,因为它本来应该放在首位。

我认为这是一个 jQuery 移动问题,但无法找出问题所在。从 python 和 Flask 的角度来看,它与我能找到的所有登录示例匹配。

【问题讨论】:

    标签: python jquery-mobile flask flask-login


    【解决方案1】:

    写完问题终于解决了。

    问题是由 jQuery mobile 和缺少 data-url 属性引起的。

    通过在页面 div 中添加 data-url 属性,浏览器中的 url 会更新并且一切正常。

    <div data-role="page" id="welcome" data-url="{{ url_for('index') }}">
    

    【讨论】:

    • 就是这样 - 只是在挖掘一些旧代码,我试图为你找到它...... :) 哦,好吧!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 2015-09-20
    相关资源
    最近更新 更多