Url in browser does not update after redirect call (url_for ('xxx')) in Flask with jQuery mobile

I have a very simple python program using Flask as shown below. It processes the login with a popup and logout. The problem is that the URL in the browser is not updated by calling 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')) 

When accessing "/", a welcome page is displayed. When I click on the button, a login popup is displayed and its form action is redirected to '/ login'. This works and the login () function is called and executed. It also redirects, but the browser does not update the display URL.

Thus, the webapp page is displayed with the URL / logon. When I click reload, I get an error message because it tries to reboot / log in while it needs to reload '/', where it was redirected.

The same thing happens with / logout. When the webapp page is displayed, and I press the exit button, the / logout page is loaded, which executes the logout () function and redirects to the index. But the url is left to log out.

If I then reload the page, it will succeed because / logout accepts the GET method, and then the URL is updated to / as it should have been in the first place.

I get the impression that this is a problem with jQuery for mobile, but cannot find the problem. From the point of view of python and Flask, it matches all the login examples I could find.

+8
python jquery-mobile flask flask-login
source share
1 answer

Finally, I decided this after completing the writing of the question.

The problem is caused by jQuery mobile and the missing data attribute attribute.

By adding the data-url attribute to the page div, the browser URL is updated and everything works fine.

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

All Articles