{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}博客{% endblock %}
{% block page_content %}
<div class="page-header">
<h1>您好, {% if current_user.is_authenticated %}{{ current_user.username }}{% else %}游客{% endif %}!</h1>
</div>
<div>
{% if current_user.can(Permission.WRITE_ARITICLES) %}
{{ wtf.quick_form(form) }}
{% endif %}
</div>
{% include '_posts.html' %}
{% endblock %}
_post.html模板为:
<ul class="posts">
{% for post in posts %}
<li class="post">
<div class="post-thumbnails">
<a href="{{ url_for('.user', username=post.author.username) }}">
<img class="img-rounded profile-thumbnails" src="{{ url_for('static', filename='avatar/') }}{{ post.author.gravatar() }}">
</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>
视图函数为:
@main.route('/', methods=['GET','POST'])
def index():
form = PostForm()
if current_user.can(Permission.WRITE_ARTICLES) and \
form.validate_on_submit():
post = Post(body = form.body.data, author=current_user._get_current_object())
db.session.add(post)
return redirect(url_for('.index'))
posts = Post.query.order_by(Post.timestamp.desc()).all()
return render_template('index.html',form=form, posts=posts)
@main.route('/user/<username>')
def user(username):
print(username)
user = User.query.filter_by(username=username).first_or_404()
posts = user.posts.order_by(Post.timestamp.desc()).all()
return render_template('user.html', user=user,posts =posts)