jinja TypeError:type Undefined不定义_______________方法

2024-06-02 07:07:39 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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显然不是一个数值吗?(这个剧本过去很管用,但我在玩弄它,它在某种程度上坏了)


Tags: inpyselfenvappflasklibpackages
1条回答
网友
1楼 · 发布于 2024-06-02 07:07:39

您是否已检查是否确实正确传递/呈现了该变量的值?如果您执行以下操作,会发生什么情况

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

我尝试将{{ test|round|int }}添加到模板中,并为test传递不同的值(int、string、float)以呈现_模板,而重现错误的方法是根本不传递值以呈现_模板

相关问题 更多 >