gpt4 book ai didi

python - 模板中的 Django Cookie 值

转载 作者:太空宇宙 更新时间:2023-11-04 03:21:46 25 4
gpt4 key购买 nike

如果请求中有特定命名的 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]

关于python - 模板中的 Django Cookie 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34463979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com