Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django request.session.get("name", False) - What does this code mean?

Tags:

python

django

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.

like image 894
user1584291 Avatar asked Aug 28 '12 19:08

user1584291


People also ask

What does session get do in Python?

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.

How can I get session data in Django?

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.

What is Session ID in Django?

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.

What is request method == post in Django?

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).


1 Answers

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
like image 97
Sean Vieira Avatar answered Nov 15 '22 17:11

Sean Vieira