jinja-TypeError:类型未定义未定义__round__方法

时间:2020-01-10 00:17:41

标签: python jinja2 typeerror

我收到以下带有错误的回溯消息:

Traceback (most recent call last):
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/benjamattesjaroen/helloPython/app.py", line 470, in nutritionrda
    return render_template('nutritionrda.html', nutritiontotals=nutritiontotals)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/templating.py", line 140, in render_template
    ctx.app,
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/templating.py", line 120, in _render
    rv = template.render(context)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/Users/benjamattesjaroen/helloPython/templates/nutritionrda.html", line 1, in top-level template code
    {% extends 'base.html' %}
  File "/Users/benjamattesjaroen/helloPython/templates/base.html", line 63, in top-level template code
    {% block body %}{% endblock %}
  File "/Users/benjamattesjaroen/helloPython/templates/nutritionrda.html", line 33, in block "body"
    <td class="col-xs-1">{{ ((nutritiontotal.calories)|round|int)}}</td><!-- Calories -->
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/jinja2/filters.py", line 797, in do_round
    return round(value, precision)
TypeError: type Undefined doesn't define __round__ method

似乎这个问题在我尝试使用Round方法后就开始了,

<td class="col-xs-1">{{ ((nutritiontotal.calories)|round|int)}}</td><!-- Calories -->

我使用单独的脚本打印了nutritiontotal.calories的值,我得到了

1718.82

那么为什么这是“类型不确定”错误? 1718.82显然不是数字值吗? (此脚本曾经可以工作,但是我一直在玩它,但它以某种方式破裂了)

1 个答案:

答案 0 :(得分:0)

您是否检查过您实际上是在正确传递/渲染该变量的值?如果您执行以下操作会怎样?

{% if nutritional.calories is defined %} 
{{ nutritional.calories|round|int }}
{% else %}
no value for nutritional.calories
{% endif %}

我尝试将{{ test|round|int }}添加到模板,并将test的不同值(int,字符串,浮点数)传递给render_template,并且重现错误的方法是不将值传递给render_template完全没有。