我正在使用 Jinja 模板引擎在 Flask 中创建一个网站。我有一个公告列表,但没有很好的方法来对它们进行排序。
他们现在正在被 id 调用的表中。我想按最新的优先排序(这意味着最高的 id)
像:
- P1
- 5
- 4
- 3
- P2
- 2
- 1
在我的 HTML 中,我为 Jinja 提供了以下说明:
<div id="main">
{% autoescape false %}
{% for announcement in announcements.items|sort(attribute='id', reverse = True) %}
<a name={{announcement.title}}></a>
<h1>{{announcement.title}} - Posted on: {{announcement.date}}</h1>
{{announcement.body}}
<br/>
<hr/>
{% endfor %}
{% endautoescape %}
<p class = "footer">{% if announcements.has_prev %}<a href="{{ url_for('index', page = announcements.prev_num) }}"><< Newer posts</a>{% else %}<< Newer posts{% endif %} |
{% if announcements.has_next %}<a href="{{ url_for('index', page = announcements.next_num) }}">Older posts >></a>{% else %}Older posts >>{% endif %} </p>
<br/>
</div>
和蟒蛇:
from flask import render_template, Markup
from app import app
from config import POSTS_PER_PAGE
from models import Announcement, VideoAnnouncement
@app.route('/')
@app.route('/index')
@app.route('/index/<int:page>')
def index(page = 1):
loggedOut = True
announcements = Announcement.query.paginate(page, POSTS_PER_PAGE, True)
videoAnnounce = VideoAnnouncement.query.all()
return render_template("index.html", announcements = announcements, videoAnnouncements = videoAnnounce , loggedOut = loggedOut)
但这有点像:
- P1
- 3
- 2
- 1
- P2
- 5
- 4
有没有按降序排序,不会被页面弄乱?
(我希望我的问题是有道理的)