From c5b02361ca02a086dfd34bc0e1df4fb1d8afd8fa Mon Sep 17 00:00:00 2001
From: tmeissner
Date: Fri, 16 Nov 2018 19:10:19 +0100
Subject: [PATCH] Chapter 11: Blog posts in profile pages (11b)
---
app/main/views.py | 3 ++-
app/templates/_posts.html | 16 ++++++++++++++++
app/templates/index.html | 18 ++----------------
app/templates/user.html | 3 +++
4 files changed, 23 insertions(+), 17 deletions(-)
create mode 100644 app/templates/_posts.html
diff --git a/app/main/views.py b/app/main/views.py
index ee19408..bced0bc 100644
--- a/app/main/views.py
+++ b/app/main/views.py
@@ -23,7 +23,8 @@ def index():
@main.route('/user/')
def user(username):
user = User.query.filter_by(username=username).first_or_404()
- return render_template('user.html', user=user)
+ posts = user.posts.order_by(Post.timestamp.desc()).all()
+ return render_template('user.html', user=user, posts=posts)
@main.route('/edit-profile', methods=['GET', 'POST'])
diff --git a/app/templates/_posts.html b/app/templates/_posts.html
new file mode 100644
index 0000000..1ca06a5
--- /dev/null
+++ b/app/templates/_posts.html
@@ -0,0 +1,16 @@
+
+ {% for post in posts %}
+ -
+
+
+
+
+
+
+
{{ moment(post.timestamp).fromNow() }}
+
+
{{ post.body }}
+
+
+ {% endfor %}
+
diff --git a/app/templates/index.html b/app/templates/index.html
index 8bc2301..4f86cca 100644
--- a/app/templates/index.html
+++ b/app/templates/index.html
@@ -14,20 +14,6 @@
{{ wtf.quick_form(form) }}
{% endif %}
-
- {% for post in posts %}
- -
-
-
-
-
-
-
-
{{ moment(post.timestamp).fromNow() }}
-
-
{{ post.body }}
-
-
- {% endfor %}
-
+
+{% include '_posts.html' %}
{% endblock %}
diff --git a/app/templates/user.html b/app/templates/user.html
index cee0191..c3b301f 100644
--- a/app/templates/user.html
+++ b/app/templates/user.html
@@ -26,6 +26,7 @@
Member since {{ moment(user.member_since).format('L') }}.
Last seen {{ moment(user.last_seen).fromNow() }}.
+
{{ user.posts.count() }} blog posts.
{% if user == current_user %}
@@ -37,4 +38,6 @@
+ Posts by {{ user.username }}
+ {% include '_posts.html' %}
{% endblock %}