Get Cookie from Request

A snippet showing how to get cookies from Django requests.


# views.py

def get_cookie(request):
    color = request.COOKIES.get('favorite_color', 'not set')
    return HttpResponse(f"Favorite color is {color}")
      
Explanation:
  • Use request.COOKIES.get('key', 'default') to get a cookie value.
  • Returns 'default' if cookie is not present.
    • Category Sessions & Cookies
    • Total Views 932
    • Last Modified 29 May, 2026
    • Tags #cookies #request #get #django
    Previous snippet
    Set Cookie in Response
    Never miss a story on Django.wiki

    Subscribe for fresh tutorials, snippets, and updates.

    By subscribing you agree to our Privacy Policy.