Simple stuff here…
if I try to reference a cookie in Django via
request.COOKIE["key"]
if the cookie doesn’t exist that will throw a key error.
For Django’s GET and POST, since they are QueryDict objects, I can just do
if "foo" in request.GET
which is wonderfully sophisticated…
what’s the closest thing to this for cookies that isn’t a Try/Catch block, if anything…
request.COOKIESis a standard Python dictionary, so the same syntax works.Another way of doing it is:
which returns the value if the key exists, otherwise ‘default’ – you can put anything you like in place of ‘default’.