I am using the following code :
if request.session.get("name",False):
Can anyone please tell me what the above code does? What I assume is, if there is "name" in session it returns True, otherwise, it returns False. I'm confused with my code so I posted this question here.
Thanks.
Session object allows one to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance and will use urllib3's connection pooling.
Using database-backed sessions If you want to use a database-backed session, you need to add 'django. contrib. sessions' to your INSTALLED_APPS setting. Once you have configured your installation, run manage.py migrate to install the single database table that stores session data.
From IDEA to Product Using Python / Django For security reasons, Django has a session framework for cookies handling. Sessions are used to abstract the receiving and sending of cookies, data is saved on server side (like in database), and the client side cookie just has a session ID for identification.
The result of request. method == "POST" is a boolean value - True if the current request from a user was performed using the HTTP "POST" method, of False otherwise (usually that means HTTP "GET", but there are also other methods).
If session
has a key in it with the value "name"
it returns the value associated with that key (which might well be False
), otherwise (if there is no key named "name") it returns False
.
The session
is a dictionary-like type so the best place to get documenation on the get
method is in the Python docs for the standard library. The short of the matter is that get
is shorthand for the following:
if "name" in request.session:
result = request.session["name"]
else:
result = False
if result:
# Do something
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