简体   繁体   中英

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().

I'm using the app factory pattern. Here's my 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_latest_posts(n) is basically the same but fetches the latest (n) posts.

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:

{% 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.

If I run this in Flask, it works without a problem. When generating static site with Frozen Flask , page numbers are there but clicking on them wouldn't redirect me anywhere. 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.

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 :

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.

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. 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/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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