Django 1.11.2
这有效:
{% if n == "1" %}
{{ n }}
{% endif %}
这不是:
{% if n=="1" %}
{{ n }}
{% endif %}
回溯:
Internal Server Error: /form/
Traceback (most recent call last):
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/smartif.py", line 175, in translate_token
op = OPERATORS[token]
KeyError: 'n=="1"'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/michael/workspace/simple_project/home/views.py", line 22, in get
{"n": n})
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/shortcuts.py", line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/loader.py", line 67, in render_to_string
template = get_template(template_name, using=using)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/loader.py", line 21, in get_template
return engine.get_template(template_name)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/backends/django.py", line 39, in get_template
return Template(self.engine.get_template(template_name), self)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/engine.py", line 162, in get_template
template, origin = self.find_template(template_name)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/engine.py", line 136, in find_template
name, template_dirs=dirs, skip=skip,
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/loaders/base.py", line 44, in get_template
contents, origin, origin.template_name, self.engine,
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/base.py", line 191, in __init__
self.nodelist = self.compile_nodelist()
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/base.py", line 230, in compile_nodelist
return parser.parse()
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/base.py", line 515, in parse
raise self.error(token, e)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/base.py", line 513, in parse
compiled_result = compile_func(self, token)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/loader_tags.py", line 312, in do_extends
nodelist = parser.parse()
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/base.py", line 515, in parse
raise self.error(token, e)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/base.py", line 513, in parse
compiled_result = compile_func(self, token)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/loader_tags.py", line 255, in do_block
nodelist = parser.parse(('endblock',))
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/base.py", line 515, in parse
raise self.error(token, e)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/base.py", line 513, in parse
compiled_result = compile_func(self, token)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/defaulttags.py", line 968, in do_if
condition = TemplateIfParser(parser, bits).parse()
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/defaulttags.py", line 900, in __init__
super(TemplateIfParser, self).__init__(*args, **kwargs)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/smartif.py", line 166, in __init__
mapped_tokens.append(self.translate_token(token))
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/smartif.py", line 177, in translate_token
return self.create_var(token)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/defaulttags.py", line 903, in create_var
return TemplateLiteral(self.template_parser.compile_filter(value), value)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/base.py", line 600, in compile_filter
return FilterExpression(token, self)
File "/home/michael/workspace/venv/simple_project/lib/python3.5/site-packages/django/template/base.py", line 700, in __init__
"from '%s'" % (token[upto:], token))
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '=="1"' from 'n=="1"'
[22/Jun/2017 08:15:21] "GET /form/?n=1 HTTP/1.1" 500 200974
文档似乎对此保持沉默。 我读过这个:https://docs.djangoproject.com/en/1.11/ref/templates/language/#tags https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#operator
你能告诉我为什么这些空间很重要。我的意思是在Django中忽略其他空格。与{{var}}和{{var}}一样。
如果空格很重要,也许我们应该在文档中强调这一点?
答案 0 :(得分:2)
这是因为模板标签和变量之间存在根本区别。模板标签接受需要用空格分隔的参数。在幕后,模板标签被解构,内容在每个空间上分开。某些模板标签可以接受不同数量的参数,但每个模板标签都有自己的实现方式。
你可以这样做(你留下第一个和最后一个空格)
{%if n == "1"%}
但不是这个
{% if n=="1" %}
因为在后一种情况下你只有一个参数。 if模板标签实际上只能接受1个参数,但它必须是一个布尔值:True或False。