Django/Jinja: "Unused is at end of expression"

Django/Jinja: "Unused is at end of expression"

访问以下 jinja 模板时出现奇怪的 Django 错误:

{% if variable is defined %}
    value of variable: {{ variable }}
{% else %}
    variable is not defined
{% endif %}

它非常基础,摘自原文 documentationvariable 没有定义也没有被提及。任何想法可能会导致此问题?

Environment:


Request Method: POST
Request URL: http://    
Django Version: 1.9.7
Python Version: 3.4.2
Installed Applications:
['medisearch',
 'mediwiki',
 'crispy_forms',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /home/django/mediwiki/medisearch/templates/medisearch/response.html, error at line 1
   Unused 'is' at end of if expression.   1 :  {% if variable is defined %} 
   2 :     value of variable: {{ variable }}
   3 : {% else %}
   4 :     variable is not defined
   5 : {% endif %}
   6 : 

Traceback:

File "/home/django/local/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/django/local/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/django/mediwiki/medisearch/views.py" in search
  21.             return render(request, 'medisearch/response.html', {'response': response})

File "/home/django/local/lib/python3.4/site-packages/django/shortcuts.py" in render
  67.             template_name, context, request=request, using=using)

File "/home/django/local/lib/python3.4/site-packages/django/template/loader.py" in render_to_string
  96.             template = get_template(template_name, using=using)

File "/home/django/local/lib/python3.4/site-packages/django/template/loader.py" in get_template
  32.                 return engine.get_template(template_name, dirs)

File "/home/django/local/lib/python3.4/site-packages/django/template/backends/django.py" in get_template
  40.             return Template(self.engine.get_template(template_name, dirs), self)

File "/home/django/local/lib/python3.4/site-packages/django/template/engine.py" in get_template
  190.         template, origin = self.find_template(template_name, dirs)

File "/home/django/local/lib/python3.4/site-packages/django/template/engine.py" in find_template
  157.                         name, template_dirs=dirs, skip=skip,

File "/home/django/local/lib/python3.4/site-packages/django/template/loaders/base.py" in get_template
  46.                     contents, origin, origin.template_name, self.engine,

File "/home/django/local/lib/python3.4/site-packages/django/template/base.py" in __init__
  189.         self.nodelist = self.compile_nodelist()

File "/home/django/local/lib/python3.4/site-packages/django/template/base.py" in compile_nodelist
  231.             return parser.parse()

File "/home/django/local/lib/python3.4/site-packages/django/template/base.py" in parse
  516.                     raise self.error(token, e)

File "/home/django/local/lib/python3.4/site-packages/django/template/base.py" in parse
  514.                     compiled_result = compile_func(self, token)

File "/home/django/local/lib/python3.4/site-packages/django/template/defaulttags.py" in do_if
  1027.     condition = TemplateIfParser(parser, bits).parse()

File "/home/django/local/lib/python3.4/site-packages/django/template/smartif.py" in parse
  201.                                    self.current_token.display())

Exception Type: TemplateSyntaxError at /medisearch/
Exception Value: Unused 'is' at end of if expression.

我猜这是因为 Django 与 Jinja2 不完全兼容。这取自 Jinja FAQ:

The default syntax of Jinja2 matches Django syntax in many ways. However this similarity doesn’t mean that you can use a Django template unmodified in Jinja2. For example filter arguments use a function call syntax rather than a colon to separate filter name and arguments. Additionally the extension interface in Jinja is fundamentally different from the Django one which means that your custom tags won’t work any longer.

当然,我不确定这是否就是它对您不起作用的原因。

不过,the Django documentation suggests using the {% if %} template tag to check for definedness(绝对是一个字):

The {% if %} tag evaluates a variable, and if that variable is “true” (i.e. exists, is not empty, and is not a false boolean value) the contents of the block are output

这里对您来说重要的是"i.e. exists"。

我最好的猜测是,因此,Django 中的 Jinja 不使用 defined 函数,因为您应该只使用 {% if %} 标签。

但是请注意,这不是常规 Python:

中的行为
if variable:
    print(variable)

# NameError: name 'variable' is not defined 

错误是因为 Django 将您的模板视为 Django 模板语言。您的 jinja2 模板属于您应用的 jinja2 目录,例如/home/django/mediwiki/medisearch/jinja2/medisearch/response.html