【发布时间】:2019-04-08 04:35:55
【问题描述】:
我有以下问题:
我目前正在学习 Flask 和 SQLAlchemy,在我的环境中我正在构建一个博客(一个简单的博客)。在我的家乡路线上,我正在执行以下操作:
@app.route("/")
@app.route("/home")
def home():
posts = Post.query.all()
return render_template("home.html", title = "Home", posts = posts)
posts = Post.query.all() 将查询我的 Posts 表上的所有字段,然后在我的模板上执行以下操作:
{% extends "layout.html" %}
{% block content %}
{% for post in posts %}
<article class="media content-section">
<img class="rounded-circle account-img" src="{{
url_for("static", filename="profile_pics/" + post.author.image_file) }}" alt="">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="#">{{ post.author.username }}</a>
<small class="text-muted">{{ post.date_posted.strftime("%Y-%m-%d") }}</small>
</div>
<h2><a class="article-title" href="#">{{ post.title }}</a></h2>
<hr>
</div>
</article>
{% endfor %}
{% endblock %}
问题是,在这个 for 循环中,我的最新帖子显示为最后一个帖子,当我真的想把它作为第一个帖子时,我如何通过 SQLAlchemy 和 Flask 反向查询数据库,或者我是想错了吗?
【问题讨论】:
标签: python python-3.x flask sqlalchemy flask-sqlalchemy