【问题标题】:Frozen-Flask pagination links doesn't load the new pageFrozen-Flask 分页链接不加载新页面
【发布时间】: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 '#' }}">&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 没什么花哨的,只是另一个包含帖子结构的模板。

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

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

【问题讨论】:

    标签: python html flask pagination jinja2


    【解决方案1】:

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

    从 URL 中删除查询字符串以构建文件名。例如, /lorem/?page=ipsum 保存到 lorem/index.html。网址仅 不同的查询字符串被认为是相同的,并且他们 应该返回相同的响应。否则,行为未定义。

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

    【讨论】:

    • 能否请您详细说明,如何将我的分页链接更改为http://127.0.0.1:5000/page2/?提前致谢!
    • 我找到了一种从页面 URL 中删除查询字符串的方法:我编辑了 main.routes.py > 到 @bp.route('/index/&lt;int:page&gt;/', methods=['GET', 'POST']) 的主路由,并使用 def index(page=1): 传递了默认页码(否则它会失败在索引上)。解决方案对我有用。所以我将此标记为答案。
    猜你喜欢
    • 2016-10-28
    • 1970-01-01
    • 2012-06-23
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2015-07-12
    • 2011-12-31
    相关资源
    最近更新 更多