I am trying to filter only the rows that have the foreign id of 1 or in this case ‘Skincare’ I have tried both, but no matter what combo I try it either applies no filter, nothing at all shows up, or I get an error that it is not iterable. I want
JavaScript
x
11
11
1
@views.route("/makeup")
2
def makeup():
3
blogs = Blog.query.filter(Blog.category_id == '1').all()
4
5
for date, img, title in blogs:
6
date = Blog.date
7
img = Blog.fileid
8
title = Blog.title
9
10
return render_template('./makeup.html', blogs = blogs)
11
JavaScript
1
32
32
1
class Category(db.Model):
2
id = db.Column(db.Integer, primary_key = True)
3
categories = db.Column(db.String(150), unique = True)
4
formss = db.relationship('Blog', backref = 'category', lazy=True)
5
6
def __repr__(self):
7
return '<Category %r>' % self.id
8
9
def __init__(self, categories):
10
self.categories = categories
11
12
13
14
class Blog(db.Model):
15
id = db.Column(db.Integer, primary_key = True)
16
title = db.Column(db.String(150), unique = True)
17
fileid = db.Column(db.String(150), unique = True)
18
date = db.Column(db.String(150))
19
content = db.Column(db.String(150))
20
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
21
22
23
def __repr__(self):
24
return '<Blog %r>' % self.id
25
26
def __init__(self, title, fileid, date, content, category):
27
self.title = title
28
self.fileid = fileid
29
self.date = date
30
self.content = content
31
self.category = category
32
JavaScript
1
7
1
{% for date, img, title in blogs %}
2
3
4
<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>
5
6
{% endfor %}
7
Advertisement
Answer
I’m not sure what is going on, it does not actually seem to matter if the id is a string or an int. Something like this should work.
JavaScript
1
7
1
@views.route("/makeup")
2
def makeup():
3
blogs = Blog.query.filter(Blog.category_id == '1').all()
4
5
# blogs is a list of model objects
6
return render_template('./makeup.html', blogs = blogs)
7
Here we iterate over the list of model objects and use the model to populate the template.
JavaScript
1
4
1
{% for blog in blogs %}
2
<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>
3
{% endfor %}
4