我在尝试使用 Jinja 模板时遇到问题,需要一些建议,我不断收到错误消息
werkzeug.routing.exceptions.BuildError:无法使用值 ['page'] 为端点 'articles' 构建 url。您的意思是“静态”吗?
这是 jinja 模板:
<div class="flex justify-center mt-10">
<ul>
<!-- previous page -->
{% if blogs.has_prev %}
<li>
<a class="text-white hover:text-gray-400"
href="{{ url_for('articles', page=blogs.prev_num) }}">Previous</a>
</li>
{% endif %}
<!-- all page numbers -->
{% for page_num in blogs.iter_pages() %}
{% if page_num %}
{% if page_num != blogs.page %}
<li>
<a href="{{ url_for('articles', page=page_num) }}">{{ page_num }}</a>
</li>
{% else %}
<li class="active">
<a class="text-white hover:text-gray-400" href="#">{{ page_num }}</a>
</li>
{% endif %}
{% else %}
<li>
<span class="ellipsis">…</span>
</li>
{% endif %}
{% endfor %}
<!-- next page -->
{% if blogs.has_next %}
<li>
<a class="text-white hover:text-gray-400"
href="{{ url_for('articles', page=blogs.next_num) }}">Next</a>
</li>
{% endif %}
</ul>
<!-- More posts... -->
</div>
这是我从中提取的烧瓶路线:
@blogs_blueprint.route("/articles", methods=["GET"])
@blogs_blueprint.route("/articles/<int:page>", methods=["GET"])
def articles(page=1):
per_page = 6
blog_list = Blog.query.order_by(desc(Blog.date_created)).paginate(page=page, per_page=per_page, error_out=False)
current_year = datetime.now().year
return render_template("articles.html", blogs=blog_list, year=current_year)
```
通常,当 Flask 应用程序不知道该路线时,就会发生类似的错误。
如果您有蓝图,您必须记住将其注册到您的 Flask 应用程序中
from . import blog # assuming the blueprint is in blog.py
app.register_blueprint(blog.blogs_blueprint)