Django / Jinja:“未使用是在表达结束时”

时间:2016-08-02 14:32:06

标签: python django jinja2

访问以下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.

2 个答案:

答案 0 :(得分:1)

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

  

Jinja2的默认语法在很多方面与Django语法相匹配。但是,这种相似性并不意味着您可以在Jinja2中使用未经修改的Django模板。例如,过滤器参数使用函数调用语法而不是冒号来分隔过滤器名称和参数。此外,Jinja中的扩展接口与Django的扩展接口根本不同,这意味着您的自定义标签将不再起作用。

当然,我不确定这是不是为什么它不适合你。

然而,the Django documentation suggests using the {% if %} template tag to check for definedness(绝对是一句话):

  

{%if%}标记计算变量,如果该变量为“true”(即存在,不为空,且不是false布尔值),则输出块的内容

这里重要的是"即。存在&#34 ;.

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

但请注意,这不是常规Python中的行为:

if variable:
    print(variable)

# NameError: name 'variable' is not defined 

答案 1 :(得分:0)

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