我需要帮助理解这一点。我昨天问了这个问题:How do I write better template logic with if statements in Django?
这是我现在的观点:
=IF(ROW()=2,1,IF(COUNTIF($A$1:A1,A2)>0,B1,B1+1))
的index.html:
def home(request):
context = {
"ignore_paths": {
"/test1/": False,
"/test2/": False,
"/test3/": False,
"/test4/": False,
"/test5/": False,
"/test6/": False,
}
}
return render(request, "index.html", context)
为什么它对我不起作用?
答案 0 :(得分:3)
您的ignore_paths
变量是字典。除非字典为空,否则它始终为True
,因此{% if not ignore_paths %}
将始终评估为False
。你想要的是检查ignore_paths
中的每个路径,如果我们True
中的任何一个路径ignore_paths
,那么如果我正确地阅读了你的其他帖子,那么这些路径就不会显示模板。
我要'ignore_paths': {'/test1/', '/test2/', '/test3/', '/test4/', '/test5/', '/test6/'}
列出您不希望显示侧边栏的路径列表或路径集:
request.path
然后使用any检查hide_sidebar = any(path in request.path for path in ignore_paths)
中是否有任何路径:
{% if not hide_sidebar %}
{% include "includes/sidebar.html" %}
{% endif %}
然后在你看来:
__DATASETS__
答案 1 :(得分:0)
它没有用,因为你没有按照预期的方式使用它....
您的原始问题是询问如何排除所有模板中的侧栏除了一对。
它打算工作的方式是在不需要显示的那些视图的上下文中包含"ignore_paths"
键,只要它不重要的值等于真。
即我使用了":)"
字符串,因为任何值的字符串都等于true。
模板然后执行艰苦的工作,因为做需要显示的那些视图的上下文中缺少“ignore_paths”键,它将成功评估if语句并输入它以包含侧栏,请参阅此处涉及的逻辑细分。
ignore_paths is included ignore_paths isn't included
{% if not ignore_paths %} {% if not ignore_paths %}
{% if not True %} {% if not False %}
{% if False %} {% if True %}
Doesn't enter if Does enter if statement