JavaEar 专注于收集分享传播有价值的技术资料

jinja2:TemplateSyntaxError:期望令牌',',得到'字符串' (jinja2: TemplateSyntaxError: expected token ',', got 'string')

I am new to Jinja2 and having an issue with using python regular expression (re). In the following code I would like to bold the lines that have error string in them.

  {% block content %}
    <div class="container">
      {% for l in lines %}
         {% if re.search(r"Error", l) %}  {# <<< Throws error #}
            <b> {{ l }} </b>
         {% else %}
            {{ l }} <hr>
         {% endif %}
      {% endfor %}
    </div>
 {% endblock %}

The re.search above throws following error:

jinja2.exceptions.TemplateSyntaxError
TemplateSyntaxError: expected token ',', got 'string'

1个回答

    最佳答案

  1. 英文原文

    Raw python code is not fully supported in jinja2 template syntax.

    {% if re.search(r"Error", l) %}
    

    replace this line with

    {% if "Error" in l %}
    

    can fix your problem.

    if your logical condition is more complicated, you should consider defining your own custom filters(which can call any python code) or do the complicated things in your view layer. also go check global namespace.


    中文翻译

    jinja2模板语法不完全支持Raw python代码。

      {%if re.search(r" Error" ,l)%}
     

    替换此行
      {%if" 错误" 在l%}
     

    可以解决您的问题。

    如果您的逻辑条件更复杂,您应该考虑定义自己的 custom过滤器(可以调用任何python代码)或在视图层中执行复杂的操作。另请查看全局命名空间

    Raw python code is not fully supported in jinja2 template syntax.

    {% if re.search(r"Error", l) %}
    

    replace this line with

    {% if "Error" in l %}
    

    can fix your problem.

    if your logical condition is more complicated, you should consider defining your own custom filters(which can call any python code) or do the complicated things in your view layer. also go check global namespace.

    jinja2模板语法不完全支持Raw python代码。

      {%if re.search(r" Error" ,l)%}
     

    替换此行
      {%if" 错误" 在l%}
     

    可以解决您的问题。

    如果您的逻辑条件更复杂,您应该考虑定义自己的 custom过滤器(可以调用任何python代码)或在视图层中执行复杂的操作。另请查看全局命名空间