I'm working through the Flask tutorial and would just like to clarify exactly what the .pop attr of the session object does and why it would take a 'None' parameter.
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('show_entries'))
To release a session variable use pop() method. The following code is a simple demonstration of session works in Flask. URL '/' simply prompts user to log in, as session variable 'username' is not set. As user browses to '/login' the login() view function, because it is called through GET method, opens up a login form.
Flask signs the data with the app's secret key when sending it, and unsigns it with the same key when reading it. Flask does not add anything to the session. There is no session id, the browser just sends the session cookie during each request, and Flask reads it.
In the flask, a session object is used to track the session data which is a dictionary object that contains a key-value pair of the session variables and their associated values. ADVERTISEMENT. ADVERTISEMENT. The following syntax is used to set the session variable to a specific value on the server.
In March 2022, The CW renewed the series for a ninth and final season. As of June 29, 2022, 171 episodes of The Flash have aired, concluding the eighth season.
According to Flask's API their Session
class is a wrapper around a python Dict
. According to the python documentation for dict.pop()
:
pop(key[, default])
If
key
is in the dictionary, remove it and return its value, else returndefault
. Ifdefault
is not given andkey
is not in the dictionary, aKeyError
is raised.
In this case the tutorial asks you to pass in None
as the default
value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With