2👍
You sould use the in
operator:
if 'member_id' in request.session:
Note that, if it exists, but is “falsy” (False
, None
, 0
, ""
etc) the in
operator will succeed, but request.session['member_id']
will not be a valid id
, so you might want to check for both:
if 'member_id' in request.session and request.session['member_id']:
Source:stackexchange.com