当我编写一个简单的Web应用程序使用烧瓶时,我遇到了一个错误。
jinja2.exceptions.UndefinedError
UndefinedError:' pagintion'未定义
下面的代码(/view.py):
page = request.args.get('page', 1, type=int)
pagination = Post.query.order_by(Post.timestamp.desc()).paginate(
page, per_page=current_app.config['FLASKY_POST_PER_PAGE'],
error_out=False)
posts = pagination.items
return render_template('index.html', form=form, posts=posts,
pagination=pagination)
为什么它让我想起未定义的分页'当我确定它。 回溯如下所示:
Traceback (most recent call last):
File "F:\Flask\test\venv\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "F:\Flask\test\venv\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "F:\Flask\test\venv\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "F:\Flask\test\venv\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "F:\Flask\test\venv\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "F:\Flask\test\venv\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "F:\Flask\test\venv\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "F:\Flask\test\venv\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "F:\***\views.py", line 26, in index
pagination=pagination)
File "F:\Flask\test\venv\lib\site-packages\flask\templating.py", line 128, in render_template
context, ctx.app)
File "F:\Flask\test\venv\lib\site-packages\flask\templating.py", line 110, in _render
rv = template.render(context)
File "F:\Flask\test\venv\lib\site-packages\jinja2\environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "F:\Flask\test\venv\lib\site-packages\jinja2\environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "F:\Github\simp1e\app\templates\index.html", line 3, in top-level template code
{% import "_macro.html" as macro %}
File "F:\Github\simp1e\app\templates\base.html", line 1, in top-level template code
{% extends "bootstrap/base.html" %}
File "F:\Flask\test\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 1, in top-level template code
{% block doc -%}
File "F:\Flask\test\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 4, in block "doc"
{%- block html %}
File "F:\Flask\test\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 20, in block "html"
{% block body -%}
File "F:\Flask\test\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 23, in block "body"
{% block content -%}
File "F:\Github\simp1e\app\templates\base.html", line 48, in block "content"
{% block page_content %}{% endblock %}
File "F:\Github\simp1e\app\templates\index.html", line 18, in block "page_content"
{{ macro.pagination_widget(pagination, '.index') }}
File "F:\Github\simp1e\app\templates\_macro.html", line 23, in template
<a href="{% if pagination.has_next %}{{ url_for(endpoint, page = pagintion.page + 1, **kwargs) }}{% else %}#{% endif%}">»</a>
File "F:\Flask\test\venv\lib\site-packages\jinja2\environment.py", line 397, in getattr
return getattr(obj, attribute)
UndefinedError: 'pagintion' is undefined
答案 0 :(得分:2)
<a href="{% if pagination.has_next %}{{ url_for(endpoint, page = pagintion.page + 1, **kwargs) }}{% else %}#{% endif%}">»</a>
你在这里写了一个错误的分页(pagintion)。