如何按日期对帖子进行排序(FLASK、PYTHON、SQLALCHEMY)
How to sort posts by date (FLASK, PYTHON, SQLALCHEMY)
我想知道如何根据上传的数据对主页上显示的 post 进行排序,我在数据库中为每个 post 保存了日期。目前它们按字母顺序显示。
这是我要显示图片的页面的路径
回家路线:
@views.route("/")
def home():
title = Post.query.get("title")
date = Post.query.get("date")
images = os.listdir(os.path.join(staticpath, "uploads"))
return render_template('home.html', images=images, user=current_user, title=title, date=date, Post=Post, User=User)
HTML 页面我想按日期显示图片
HTML 页数:
{% extends "base.html" %}
{% block title %}home{% endblock %}
{% block content %}
<section class="list">
{% for image in images %}
<p> title: {{ Post.query.filter_by(name=image).first().title }}</p>
<p> date: {{ Post.query.filter_by(name=image).first().date}}</p>
<p> OP ID: {{ Post.query.filter_by(name=image).first().user_id}}
<section class="col-md-3 col-sm-6" >
<img src="{{ url_for('static', filename='uploads/' + image) }}"width = 530>
</section>
<a href="{{ url_for('static', filename='uploads/' + image) }}" style="absolute: 600;" download>{{ image }}</a>
<p class="solid" style="border-style: solid;"></p>
{% endfor %}
</section>
{% endblock %}
这是存储post个的数据库,它包括日期
数据库模型:
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
date = db.Column(db.DateTime(timezone=True), default = func.now())
minetype = db.Column(db.Text, nullable=False)
name = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
我将按以下方式更改处理程序和模板,这将使您能够实现:
- 所有
Post
将在一个查询中加载,并立即排序
- 您将不会在模板中进行数据库查询,从而更清晰地分离关注点
def home():
title = Post.query.get("title")
date = Post.query.get("date")
# images below are list of strings
images = os.listdir(os.path.join(staticpath, "uploads"))
# images below is a collection of `Post` instances (and it is sorted)
images = Post.query.filter(Post.name.in_(images)).order_by(Post.date)
return render_template('home.html', images=images, user=current_user, title=title, date=date, Post=Post, User=User)
您的模板将更改如下:
{% extends "base.html" %}
{% block title %}home{% endblock %}
{% block content %}
<section class="list">
{% for image in images %}
<p> title: {{ image.title }}</p>
<p> date: {{ image.date }}</p>
<p> OP ID: {{ image.user_id }}</p>
<section class="col-md-3 col-sm-6" >
<img src="{{ url_for('static', filename='uploads/' + image.name) }}" width = 530>
</section>
<a href="{{ url_for('static', filename='uploads/' + image.name) }}" style="absolute: 600;" download>{{ image }}</a>
<p class="solid" style="border-style: solid;"></p>
{% endfor %}
</section>
{% endblock %}
我想知道如何根据上传的数据对主页上显示的 post 进行排序,我在数据库中为每个 post 保存了日期。目前它们按字母顺序显示。
这是我要显示图片的页面的路径
回家路线:
@views.route("/")
def home():
title = Post.query.get("title")
date = Post.query.get("date")
images = os.listdir(os.path.join(staticpath, "uploads"))
return render_template('home.html', images=images, user=current_user, title=title, date=date, Post=Post, User=User)
HTML 页面我想按日期显示图片
HTML 页数:
{% extends "base.html" %}
{% block title %}home{% endblock %}
{% block content %}
<section class="list">
{% for image in images %}
<p> title: {{ Post.query.filter_by(name=image).first().title }}</p>
<p> date: {{ Post.query.filter_by(name=image).first().date}}</p>
<p> OP ID: {{ Post.query.filter_by(name=image).first().user_id}}
<section class="col-md-3 col-sm-6" >
<img src="{{ url_for('static', filename='uploads/' + image) }}"width = 530>
</section>
<a href="{{ url_for('static', filename='uploads/' + image) }}" style="absolute: 600;" download>{{ image }}</a>
<p class="solid" style="border-style: solid;"></p>
{% endfor %}
</section>
{% endblock %}
这是存储post个的数据库,它包括日期
数据库模型:
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
date = db.Column(db.DateTime(timezone=True), default = func.now())
minetype = db.Column(db.Text, nullable=False)
name = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
我将按以下方式更改处理程序和模板,这将使您能够实现:
- 所有
Post
将在一个查询中加载,并立即排序 - 您将不会在模板中进行数据库查询,从而更清晰地分离关注点
def home():
title = Post.query.get("title")
date = Post.query.get("date")
# images below are list of strings
images = os.listdir(os.path.join(staticpath, "uploads"))
# images below is a collection of `Post` instances (and it is sorted)
images = Post.query.filter(Post.name.in_(images)).order_by(Post.date)
return render_template('home.html', images=images, user=current_user, title=title, date=date, Post=Post, User=User)
您的模板将更改如下:
{% extends "base.html" %}
{% block title %}home{% endblock %}
{% block content %}
<section class="list">
{% for image in images %}
<p> title: {{ image.title }}</p>
<p> date: {{ image.date }}</p>
<p> OP ID: {{ image.user_id }}</p>
<section class="col-md-3 col-sm-6" >
<img src="{{ url_for('static', filename='uploads/' + image.name) }}" width = 530>
</section>
<a href="{{ url_for('static', filename='uploads/' + image.name) }}" style="absolute: 600;" download>{{ image }}</a>
<p class="solid" style="border-style: solid;"></p>
{% endfor %}
</section>
{% endblock %}