TeglaTheOne
TeglaTheOne

Reputation: 53

Django Cookie Value inside template

I want to show additional links in the layout, if there is a certain named cookie inside the request. I've managed to access 'request' variable inside the template. I made the following:

 {% if request.COOKIES.['cookie_name'] %}
   <li><a href="{% url 'felhasznalo:felhasznalo-detail' %}">some link</a></li>
 {% endif  %}

but it throws this error: TemplateSyntaxError at / Could not parse the remainder: '['cookie_name']' from 'request.COOKIES.['cookie_name']'

what am I doing wrong? (the cookie is exists, and if i print out request.COOKIES, it writes out all the containing cookie's names, and it's values)

Upvotes: 2

Views: 3033

Answers (1)

maazza
maazza

Reputation: 7183

https://docs.djangoproject.com/en/1.11/ref/templates/api/#variables-and-lookups

use

{% if request.COOKIES.cookie_name %}
   <li><a href="{% url 'felhasznalo:felhasznalo-detail' %}">some link</a></li>
{% endif  %}

Variables and lookups

Variable names must consist of any letter (A-Z), any digit (0-9), an underscore (but they must not start with an underscore) or a dot.

Dots have a special meaning in template rendering. A dot in a variable name signifies a lookup. Specifically, when the template system encounters a dot in a variable name, it tries the following lookups, in this order:

Dictionary lookup. Example: foo["bar"]
Attribute lookup. Example: foo.bar
List-index lookup. Example: foo[bar]

Upvotes: 4

Related Questions