Python 带分页的独立柱布线-烧瓶
目标:发送到一篇文章/博客文章,并包括分页(使用SQL Alchemy),以便用户可以轻松导航到以前和将来的文章 目前,我有以下几点:Python 带分页的独立柱布线-烧瓶,python,pagination,flask,sqlalchemy,flask-sqlalchemy,Python,Pagination,Flask,Sqlalchemy,Flask Sqlalchemy,目标:发送到一篇文章/博客文章,并包括分页(使用SQL Alchemy),以便用户可以轻松导航到以前和将来的文章 目前,我有以下几点: @app.route('/articles/ind/<int:id>', endpoint='article') def indarticle(id = 1): posts= Post.query.paginate(id, 1, False) return render_template('article.html',
@app.route('/articles/ind/<int:id>', endpoint='article')
def indarticle(id = 1):
posts= Post.query.paginate(id, 1, False)
return render_template('article.html',
title = "Article",
posts = posts)
这样,我可以得到下一篇文章,上一篇文章,也可以很容易地检查它们是否存在,就像Alchemy如何处理分页一样。为了确保我们讨论的是同一件事,分页有两种类型 首先,烧瓶支撑的那种。。。。调用所有文章并显示每页X篇文章 第二,你在一个岗位上,想要前进和后退 听起来你想做第二种 我所做的是在我的帖子模型中编写两个staticmethods,计算出上一篇和下一篇帖子。。。然后,我通过查看函数将值传递给模板 模型后静态方法 型号
@staticmethod
def prev_post(pid):
count = Post.get_count()
while (count > 0):
prev_post = None
pid = pid - 1
if db.session.query(Post).get(pid) is not None:
prev_post = db.session.query(Post).get(pid)
break
else:
count = count - 1
return prev_post
@staticmethod
def next_post(pid):
count = Post.get_count()
while (count > 0):
next_post = None
pid = pid + 1
if db.session.query(Post).get(pid) is not None:
next_post = db.session.query(Post).get(pid)
break
else:
count = count - 1
return next_post
prev_post功能的工作原理
@app.route('/<path:url>', methods=['GET', 'POST']) def
display_post(url):
post = Post.get_post(url)
next_post = Post.next_post(post.id)
prev_post = Post.prev_post(post.id)
return render_template('post.html',
next=next_post, prev=prev_post, post=post)
@app.route('/',methods=['GET','POST'])def
显示帖子(url):
post=post.get\u post(url)
下一个职位=职位。下一个职位(职位id)
上一篇文章=上一篇文章(文章id)
返回呈现模板('post.html',
下一个=下一个职位,上一个=上一个职位,职位=职位)
这是模板
模板
{% extends "base.html" %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<H2>{{ post.title }}</H2>
<b>Posted {{ post.pub_date|dts }} in <a href="{{ url_for('display_topic', url=post.topic.url) }}">{{post.topic}}</a></b>
{{post.body|markdown}}
{% if prev.url is defined %}
<a href="{{ url_for('display_post', url=prev.url) }}"><<< {{prev.title}}</a>
{% endif %}
{% if prev.url is not defined and next.url is not defined %}{% else %} | {% endif %}
{% if next.url is defined %}
<a href="{{ url_for('display_post', url=next.url) }}">{{next.title}} >>></a>
{% endif %}
{% if current_user.is_authenticated() %}
<p><a href="{{ url_for('edit_post', url=post.url) }}">edit</a></p>
<p><a href="{{ url_for('delete_post', url=post.url) }}">delete</a></p>
{% endif %}
{% endblock %}
{%extends“base.html”%}
{%block title%}{{post.title}{%endblock%}
{%block content%}
{{post.title}}
在中发布{post.pub_date}dts}
{{post.body | markdown}}
{%如果定义了prev.url%}
{%endif%}
{%如果当前用户是经过身份验证的用户。{u()%}
{%endif%}
{%endblock%}
模板仅在存在下一篇或上一篇文章时显示链接。我喜欢这个策略;不过,我对它做了一些更改/升级,使它更像flask sqlalchemy中的分页功能!将很快发布!酷。请查看**if db.session.query(Post.get)(pid)不是None:*行。它们可能会得到改进。同时,如果您认为问题得到了恰当的回答,请关闭该问题。您可以稍后更新您的回复。
{% extends "base.html" %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<H2>{{ post.title }}</H2>
<b>Posted {{ post.pub_date|dts }} in <a href="{{ url_for('display_topic', url=post.topic.url) }}">{{post.topic}}</a></b>
{{post.body|markdown}}
{% if prev.url is defined %}
<a href="{{ url_for('display_post', url=prev.url) }}"><<< {{prev.title}}</a>
{% endif %}
{% if prev.url is not defined and next.url is not defined %}{% else %} | {% endif %}
{% if next.url is defined %}
<a href="{{ url_for('display_post', url=next.url) }}">{{next.title}} >>></a>
{% endif %}
{% if current_user.is_authenticated() %}
<p><a href="{{ url_for('edit_post', url=post.url) }}">edit</a></p>
<p><a href="{{ url_for('delete_post', url=post.url) }}">delete</a></p>
{% endif %}
{% endblock %}