如果请求中有特定命名的 cookie,我想在布局中显示其他链接。我设法访问了模板内的“请求”变量。我做了以下内容:
{% if request.COOKIES.['cookie_name'] %}
<li><a href="{% url 'felhasznalo:felhasznalo-detail' %}">some link</a></li>
{% endif %}
但是它抛出这个错误:TemplateSyntaxError at/ 无法解析剩余部分:来自“request.COOKIES.['cookie_name']'的'['cookie_name']'
我做错了什么? (cookie 存在,如果我打印出 request.COOKIES,它会写出所有包含 cookie 的名称及其值)
请您参考如下方法:
https://docs.djangoproject.com/en/1.11/ref/templates/api/#variables-and-lookups
使用
{% 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]
