【发布时间】:2020-02-12 06:05:54
【问题描述】:
我收到以下带有错误的回溯消息:
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
似乎问题在我尝试使用圆形方法时就开始了,在这里
<td class="col-xs-1">{{ ((nutritiontotal.calories)|round|int)}}</td><!-- Calories -->
我使用单独的脚本打印了nutritiontotal.calories 的值,我得到了
1718.82
那么为什么这是一个“类型未定义”错误? 1718.82 显然不是一个数值吗? (这个脚本曾经可以工作,但我正在玩它,它以某种方式坏了)
【问题讨论】:
-
你试过不带括号吗?类似
{{ nutritiontotal.calories|round|int }}。