Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django read cookie in template tag

Tags:

python

django

Is it posible to read a cookie in a template tag?

I set the cookie in a middleware but I'd like to read in a template tag.

def process_response(self, request, response):
    response.set_cookie('changed', 'yes')
    response.set_cookie('choose', request.LANGUAGE_CODE)
    return response

Thanks

like image 904
user3657840 Avatar asked Dec 07 '22 00:12

user3657840


1 Answers

Since cookies are key/value pairs, you can read the value of a cookie in a template using the dot notation as shown below.

In your views:

def process_response(self, request, response):
response.set_cookie('changed', 'yes')
response.set_cookie('choose', request.LANGUAGE_CODE)
return response

And in your template:

{{ request.COOKIES.cookie_name }}
like image 101
Hmatrix Avatar answered Dec 10 '22 12:12

Hmatrix