如何解决冻结-Flask分页链接不会加载新页面
我正在尝试使用Frozen Flask冻结我的Flask博客应用程序,但问题是在Frozen()之后我无法使分页正常工作。
我正在使用应用程序工厂模式。 这是我的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/
更改为,但新内容没有加载,仅刷新了当前页面。
这里可能是什么问题?如何正确加载页面和分页?
解决方法
根据the Frozen Flask documentation on how the filenames are generated:
从URL中删除查询字符串以构建文件名。例如,
/lorem/?page=ipsum
被保存到lorem/index.html
。仅URL 不同的查询字符串被认为是相同的,并且 应该返回相同的响应。否则,行为是不确定的。
不幸的是,这意味着http://127.0.0.1:5000/
和http://127.0.0.1:5000/?page=2
将引用完全相同的页面。要使分页生效,您需要确保页码是查询字符串之前URL的一部分-类似于http://127.0.0.1:5000/page2/
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。