Skip to content

Commit f9e8733

Browse files
Chapter 11: Blog posts in profile pages (11b)
1 parent cb7face commit f9e8733

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

app/main/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def index():
2323
@main.route('/user/<username>')
2424
def user(username):
2525
user = User.query.filter_by(username=username).first_or_404()
26-
return render_template('user.html', user=user)
26+
posts = user.posts.order_by(Post.timestamp.desc()).all()
27+
return render_template('user.html', user=user, posts=posts)
2728

2829

2930
@main.route('/edit-profile', methods=['GET', 'POST'])

app/templates/_posts.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<ul class="posts">
2+
{% for post in posts %}
3+
<li class="post">
4+
<div class="post-thumbnail">
5+
<a href="{{ url_for('.user', username=post.author.username) }}">
6+
<img class="img-rounded profile-thumbnail" src="{{ post.author.gravatar(size=40) }}">
7+
</a>
8+
</div>
9+
<div class="post-content">
10+
<div class="post-date">{{ moment(post.timestamp).fromNow() }}</div>
11+
<div class="post-author"><a href="{{ url_for('.user', username=post.author.username) }}">{{ post.author.username }}</a></div>
12+
<div class="post-body">{{ post.body }}</div>
13+
</div>
14+
</li>
15+
{% endfor %}
16+
</ul>

app/templates/index.html

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,5 @@ <h1>Hello, {% if current_user.is_authenticated %}{{ current_user.username }}{% e
1212
{{ wtf.quick_form(form) }}
1313
{% endif %}
1414
</div>
15-
<ul class="posts">
16-
{% for post in posts %}
17-
<li class="post">
18-
<div class="post-thumbnail">
19-
<a href="{{ url_for('.user', username=post.author.username) }}">
20-
<img class="img-rounded profile-thumbnail" src="{{ post.author.gravatar(size=40) }}">
21-
</a>
22-
</div>
23-
<div class="post-content">
24-
<div class="post-date">{{ moment(post.timestamp).fromNow() }}</div>
25-
<div class="post-author"><a href="{{ url_for('.user', username=post.author.username) }}">{{ post.author.username }}</a></div>
26-
<div class="post-body">{{ post.body }}</div>
27-
</div>
28-
</li>
29-
{% endfor %}
30-
</ul>
15+
{% include '_posts.html' %}
3116
{% endblock %}

app/templates/user.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ <h1>{{ user.username }}</h1>
2020
{% endif %}
2121
{% if user.about_me %}<p>{{ user.about_me }}</p>{% endif %}
2222
<p>Member since {{ moment(user.member_since).format('L') }}. Last seen {{ moment(user.last_seen).fromNow() }}.</p>
23+
<p>{{ user.posts.count() }} blog posts.</p>
2324
<p>
2425
{% if user == current_user %}
2526
<a class="btn btn-default" href="{{ url_for('.edit_profile') }}">Edit Profile</a>
@@ -30,4 +31,6 @@ <h1>{{ user.username }}</h1>
3031
</p>
3132
</div>
3233
</div>
34+
<h3>Posts by {{ user.username }}</h3>
35+
{% include '_posts.html' %}
3336
{% endblock %}

0 commit comments

Comments
 (0)