简体   繁体   English

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

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

I am trying to freeze my Flask blog app with Frozen Flask but the problem is I can't get the pagination to work correctly after the freeze().我正在尝试使用 Frozen Flask 冻结我的 Flask 博客应用程序,但问题是我无法在冻结()后使分页正常工作。

I'm using the app factory pattern.我正在使用应用程序工厂模式。 Here's my main.routes.py:这是我的 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() does what is says, returning Post.query.order_by(Post.pub_date.desc()) load_all_posts()做所说的,返回Post.query.order_by(Post.pub_date.desc())

load_latest_posts(n) is basically the same but fetches the latest (n) posts. load_latest_posts(n)基本相同,但获取最新的(n)个帖子。

As you see, I'm passing the pagination object to posts which I use in my main/index.html template to render the pagination items:如您所见,我将pagination对象传递给我在main/index.html模板中用于呈现分页项的posts

{% 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 is nothing fancy, just another template that includes post structure. _post.html没什么特别的,只是另一个包含帖子结构的模板。

If I run this in Flask, it works without a problem.如果我在 Flask 中运行它,它可以毫无问题地工作。 When generating static site with Frozen Flask , page numbers are there but clicking on them wouldn't redirect me anywhere.使用Frozen Flask生成静态站点时,页码在那里,但单击它们不会将我重定向到任何地方。 I see the URL being changed from http://127.0.0.1:5000/ to http://127.0.0.1:5000/?page=2 but the new content doesn't load, only refreshing the current page.我看到URLhttp://127.0.0.1:5000/更改http://127.0.0.1:5000/?page=2但新内容未加载,仅刷新当前页面。

What might be the issue here ?这里可能有什么问题? How can I load pages and pagination correctly?如何正确加载页面和分页?

According tothe Frozen Flask documentation on how the filenames are generated :根据有关如何生成文件名的 Frozen Flask 文档

Query strings are removed from URLs to build filenames.从 URL 中删除查询字符串以构建文件名。 For example, /lorem/?page=ipsum is saved to lorem/index.html .例如, /lorem/?page=ipsum被保存到lorem/index.html URLs that are only different by their query strings are considered the same, and they should return the same response.仅查询字符串不同的 URL 被认为是相同的,并且它们应该返回相同的响应。 Otherwise, the behavior is undefined.否则,行为是未定义的。

This means that, unfortunately, http://127.0.0.1:5000/ and http://127.0.0.1:5000/?page=2 will refer to exactly the same page.这意味着,不幸的是, http://127.0.0.1:5000/http://127.0.0.1:5000/?page=2将引用完全相同的页面。 To get pagination to work, you'd need to make sure that the page number was part of the URL before the query string - something like http://127.0.0.1:5000/page2/ .要使分页工作,您需要确保页码是查询字符串之前的 URL 的一部分 - 类似于http://127.0.0.1:5000/page2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM