56π
β
I assume that you want to check if a key is set in session, not if a session is set (donβt know what the latter means). If so:
You can do:
if key not in request.session:
# Set it.
In your case:
if 'cart' not in request.session:
# Set it.
EDIT: changed the code snippet to use key not in
rather than not key in
. Thanks @katrielalex.
π€Manoj Govindan
21π
You can use the get
-method on the session dictionary, it will not throw an error if the key doesnβt exist, but return none as a default value or your custom default value:
cart = request.session.get('cart')
cart = request.session.get('cart', 'no cart')
π€Bernhard Vallant
- [Django]-Pypi see older versions of package
- [Django]-Django rest framework change primary key to use a unqiue field
- [Django]-How to use Django ImageField, and why use it at all?
Source:stackexchange.com