无法查询和遍历数据库
Can't Query and iterate through database
我试图只过滤外部 ID 为 1 的行,或者在这种情况下 'Skincare' 我都试过了,但无论我尝试什么组合,要么不应用过滤器,要么什么都不应用出现,或者我得到一个错误,它是不可迭代的。我要
@views.route("/makeup")
def makeup():
blogs = Blog.query.filter(Blog.category_id == '1').all()
for date, img, title in blogs:
date = Blog.date
img = Blog.fileid
title = Blog.title
return render_template('./makeup.html', blogs = blogs)
class Category(db.Model):
id = db.Column(db.Integer, primary_key = True)
categories = db.Column(db.String(150), unique = True)
formss = db.relationship('Blog', backref = 'category', lazy=True)
def __repr__(self):
return '<Category %r>' % self.id
def __init__(self, categories):
self.categories = categories
class Blog(db.Model):
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(150), unique = True)
fileid = db.Column(db.String(150), unique = True)
date = db.Column(db.String(150))
content = db.Column(db.String(150))
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
def __repr__(self):
return '<Blog %r>' % self.id
def __init__(self, title, fileid, date, content, category):
self.title = title
self.fileid = fileid
self.date = date
self.content = content
self.category = category
{% for date, img, title in blogs %}
<div class="fade1"><div class="fade"><img class="fadeimg" src="{{url_for('static', filename='uploads/')}}{{img}}"><button class="btn">View Post</button></div><p>{{date}}</p><h2>{{title}}</h2></div>
{% endfor %}
我不确定发生了什么,id 是字符串还是整数似乎并不重要。像这样的东西应该有用。
@views.route("/makeup")
def makeup():
blogs = Blog.query.filter(Blog.category_id == '1').all()
# blogs is a list of model objects
return render_template('./makeup.html', blogs = blogs)
这里我们遍历模型对象列表并使用模型填充模板。
{% for blog in blogs %}
<div class="fade1"><div class="fade"><img class="fadeimg" src="{{url_for('static', filename='uploads/')}}{{ blog.fileid }}"><button class="btn">View Post</button></div><p>{{ blog.date }}</p><h2>{{ blog.title }}</h2></div>
{% endfor %}
我试图只过滤外部 ID 为 1 的行,或者在这种情况下 'Skincare' 我都试过了,但无论我尝试什么组合,要么不应用过滤器,要么什么都不应用出现,或者我得到一个错误,它是不可迭代的。我要
@views.route("/makeup")
def makeup():
blogs = Blog.query.filter(Blog.category_id == '1').all()
for date, img, title in blogs:
date = Blog.date
img = Blog.fileid
title = Blog.title
return render_template('./makeup.html', blogs = blogs)
class Category(db.Model):
id = db.Column(db.Integer, primary_key = True)
categories = db.Column(db.String(150), unique = True)
formss = db.relationship('Blog', backref = 'category', lazy=True)
def __repr__(self):
return '<Category %r>' % self.id
def __init__(self, categories):
self.categories = categories
class Blog(db.Model):
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(150), unique = True)
fileid = db.Column(db.String(150), unique = True)
date = db.Column(db.String(150))
content = db.Column(db.String(150))
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
def __repr__(self):
return '<Blog %r>' % self.id
def __init__(self, title, fileid, date, content, category):
self.title = title
self.fileid = fileid
self.date = date
self.content = content
self.category = category
{% for date, img, title in blogs %}
<div class="fade1"><div class="fade"><img class="fadeimg" src="{{url_for('static', filename='uploads/')}}{{img}}"><button class="btn">View Post</button></div><p>{{date}}</p><h2>{{title}}</h2></div>
{% endfor %}
我不确定发生了什么,id 是字符串还是整数似乎并不重要。像这样的东西应该有用。
@views.route("/makeup")
def makeup():
blogs = Blog.query.filter(Blog.category_id == '1').all()
# blogs is a list of model objects
return render_template('./makeup.html', blogs = blogs)
这里我们遍历模型对象列表并使用模型填充模板。
{% for blog in blogs %}
<div class="fade1"><div class="fade"><img class="fadeimg" src="{{url_for('static', filename='uploads/')}}{{ blog.fileid }}"><button class="btn">View Post</button></div><p>{{ blog.date }}</p><h2>{{ blog.title }}</h2></div>
{% endfor %}