Frozen-Flask 分页链接不会加载新页面

Frozen-Flask pagination links doesn't load the new page

我正在尝试使用 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) posts.

如您所见,我将 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 '#' }}">&laquo;</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 '#' }}">&raquo;</a></li>
  {% endif %}
</ul>
{% endblock footer %}

_post.html 没什么特别的,只是另一个包含 post 结构的模板。

如果我在 Flask 中 运行 这个,它可以正常工作。使用 Frozen Flask 生成静态站点时,页码在那里,但单击它们不会将我重定向到任何地方。我看到 URL 被更改http://127.0.0.1:5000/http://127.0.0.1:5000/?page=2 但是新内容没有加载,只刷新当前页面。

这可能是什么问题?如何正确加载页面和分页?

根据the Frozen Flask documentation on how the filenames are generated

Query strings are removed from URLs to build filenames. For example, /lorem/?page=ipsum is saved to lorem/index.html. URLs that are only different by their query strings are considered the same, and they should return the same response. Otherwise, the behavior is undefined.

这意味着,不幸的是,http://127.0.0.1:5000/http://127.0.0.1:5000/?page=2 将引用完全相同的页面。要使分页正常工作,您需要确保页码是查询字符串之前 URL 的一部分 - 类似于 http://127.0.0.1:5000/page2/.