Browse Source

Chapter 11: Blog posts in profile pages (11b)

master
T. Meissner 6 years ago
parent
commit
c5b02361ca
4 changed files with 23 additions and 17 deletions
  1. +2
    -1
      app/main/views.py
  2. +16
    -0
      app/templates/_posts.html
  3. +2
    -16
      app/templates/index.html
  4. +3
    -0
      app/templates/user.html

+ 2
- 1
app/main/views.py View File

@ -23,7 +23,8 @@ def index():
@main.route('/user/<username>')
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'])


+ 16
- 0
app/templates/_posts.html View File

@ -0,0 +1,16 @@
<ul class="posts">
{% for post in posts %}
<li class="post">
<div class="post-thumbnail">
<a href="{{ url_for('.user', username=post.author.username) }}">
<img class="img-rounded profile-thumbnail" src="{{ post.author.gravatar(size=40) }}">
</a>
</div>
<div class="post-content">
<div class="post-date">{{ moment(post.timestamp).fromNow() }}</div>
<div class="post-author"><a href="{{ url_for('.user', username=post.author.username) }}">{{ post.author.username }}</a></div>
<div class="post-body">{{ post.body }}</div>
</div>
</li>
{% endfor %}
</ul>

+ 2
- 16
app/templates/index.html View File

@ -14,20 +14,6 @@
{{ wtf.quick_form(form) }}
{% endif %}
</div>
<ul class="posts">
{% for post in posts %}
<li class="post">
<div class="post-thumbnail">
<a href="{{ url_for('.user', username=post.author.username) }}">
<img class="img-rounded profile-thumbnail" src="{{ post.author.gravatar(size=40) }}">
</a>
</div>
<div class="post-content">
<div class="post-date">{{ moment(post.timestamp).fromNow() }}</div>
<div class="post-author"><a href="{{ url_for('.user', username=post.author.username) }}">{{ post.author.username }}</a></div>
<div class="post-body">{{ post.body }}</div>
</div>
</li>
{% endfor %}
</ul>
{% include '_posts.html' %}
{% endblock %}

+ 3
- 0
app/templates/user.html View File

@ -26,6 +26,7 @@
<p>
Member since {{ moment(user.member_since).format('L') }}.
Last seen {{ moment(user.last_seen).fromNow() }}.
<p>{{ user.posts.count() }} blog posts.</p>
</p>
<p>
{% if user == current_user %}
@ -37,4 +38,6 @@
</p>
</div>
</div>
<h3>Posts by {{ user.username }}</h3>
{% include '_posts.html' %}
{% endblock %}

Loading…
Cancel
Save