【发布时间】:2020-12-20 01:02:40
【问题描述】:
我正在尝试使用 Frozen Flask 冻结我的 Flask 博客应用程序,但问题是我无法在 freeze() 后使分页正常工作。
我正在使用应用工厂模式。 这是我的 main.routes.py:
@bp.route('/home')
@bp.route('/index')
@bp.route('/')
def index(form=None, methods=['GET', 'POST']):
latest_posts = load_latest_posts(10)
with db_session(autocommit=False):
page = 1
posts = load_all_posts().paginate(page, 10, False)
next_url = url_for('main.index', page=posts.next_num) \
if posts.has_next else None
prev_url = url_for('main.index', page=posts.prev_num) \
if posts.has_prev else None
if current_user.is_anonymous:
return render_template('main/index.html', title='Home', posts = posts,
prev_url=prev_url, next_url=next_url, latest_posts=latest_posts)
load_all_posts() 照说的做,返回Post.query.order_by(Post.pub_date.desc())
load_latest_posts(n) 基本相同,但会获取最新的(n) 帖子。
如您所见,我将pagination 对象传递给posts,我在main/index.html 模板中使用它来呈现分页项:
{% extends 'base.html' %}
{% block posts_preview %}
{% for post in posts.items %}
{% include 'posts/_post.html' %}
{% endfor %}
{% endblock posts_preview %}
{% block footer %}
<ul class="pagination">
{% if prev_url %}
<li><a href="{{ prev_url or '#' }}">«</a></li>
{% endif %}
{% for page_num in posts.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=3) %}
{% if page_num %}
{% if posts.page == page_num %}
<li><a class="active" href="{{url_for('main.index', page=page_num) }}">{{ page_num }}</a></li>
{% else %}
<li><a href="{{url_for('main.index', page=page_num) }}">{{ page_num }}</a></li>
{% endif %}
{% else %}
...
{% endif %}
{% endfor %}
{% if next_url %}
<li><a href="{{ next_url or '#' }}">»</a></li>
{% endif %}
</ul>
{% endblock footer %}
_post.html 没什么花哨的,只是另一个包含帖子结构的模板。
如果我在 Flask 中运行它,它可以正常工作。使用 Frozen Flask 生成静态站点时,页码在那里,但点击它们不会将我重定向到任何地方。我看到 URL 正在更改从 http://127.0.0.1:5000/ 到 http://127.0.0.1:5000/?page=2,但新内容没有加载,仅刷新当前页面。
这里可能是什么问题?如何正确加载页面和分页?
【问题讨论】:
标签: python html flask pagination jinja2