【问题标题】:Python: Unknown Jinja error causing script not to runPython:未知的 Jinja 错误导致脚本无法运行
【发布时间】:2020-03-10 15:29:27
【问题描述】:

我正在尝试运行以下 python 命令: $ python stack.py <file_name> 我位于包含 stack.py 文件的正确文件夹中。

但是,我收到以下错误,我不确定如何解决它或为什么会出现这种情况。

我尝试升级 jinja $ pip install Jinja2,但没有成功。

我还查看了 stack_error_vals.json 和 stack_error_template.j2,但其中没有有用的信息。

关于如何解决这个问题以便我的 python 命令再次成功的任何想法?

  File "stack.py", line 514, in proc_jinja_templates
    output = env.render(vals)
  File "C:\Users\jthomas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "C:\Users\jthomas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\jinja2\environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "C:\Users\jthomas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\jthomas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\jinja2\_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 16, in top-level template code
  File "C:/carbon\modules\templates\bridge\configs\config.xml.j2", line 27, in top-level template code
    {%- if v.required or ((v.configdatatype == 'Bool' and v.value in (true,false)) or (v.configdatatype == 'String' and v.value and 'changeme' not in v.value.lower())) -%}
jinja2.exceptions.UndefinedError: 'int object' has no attribute 'lower'

Writing stack_error_vals.json and stack_error_template.j2 for review

【问题讨论】:

    标签: python python-3.x git jinja2 git-bash


    【解决方案1】:

    看起来您的模板假设 v.value 是一个字符串,但在这种情况下,它得到的东西看起来像一个整数。

    【讨论】:

      【解决方案2】:
      jinja2.exceptions.UndefinedError: 'int object' has no attribute 'lower'
      

      这意味着您尝试应用.lower() 的对象是整数而不是字符串。

      没有看到你的完整代码,你可以试试

      str(v.value).lower()
      

      【讨论】:

      • 在config.xml.j2文件中,第27行有{%- if v.required or ((v.configdatatype == 'Bool' and v.value in (true,false)) or (v.configdatatype == 'String' and v.value and 'changeme' not in v.value.lower())) -%}
      猜你喜欢
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多