Python jinja-TypeError:type未定义不';t定义圆方法
我收到以下带有错误的回溯消息:Python jinja-TypeError:type未定义不';t定义圆方法,python,jinja2,typeerror,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/h
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
那么,为什么这是一个“类型未定义”错误呢?
1718.82
显然不是一个数值吗?(这个脚本以前可以工作,但我一直在玩弄它,它在某种程度上坏了)您是否检查过是否确实正确地传递/呈现了该变量的值?如果您执行以下操作,会发生什么情况
{% if nutritional.calories is defined %}
{{ nutritional.calories|round|int }}
{% else %}
no value for nutritional.calories
{% endif %}
我尝试将
{{test | round | int}}
添加到模板中,并为test
(int、string、float)传递不同的值以渲染_模板,而重现错误的方法是根本不传递值以渲染_模板。您尝试过没有这些括号吗?类似于{{nutritiontotal.carries | round | int}}
。
1718.82
{% if nutritional.calories is defined %}
{{ nutritional.calories|round|int }}
{% else %}
no value for nutritional.calories
{% endif %}