我们有时候希望和朋友分享自己的博客, 所以我们要为每一个博客添加一个唯一的链接以供分享使用。
一. 修改|-app/-main/views.py
@main.route('/post/<int:id>')
def post(id):
post = Post.query.get_or_404(id)
return render_template('post.html', posts=[post])
二. 修改|-app/templates/post.html
{% extends "base.html" %}
{% block title %}Flasky - Post{% endblock %}
{% block page_content %}
{% include '_posts.html' %}
{% endblock %}
三. 修改|-app/templates/_post.html
<ul class="posts">
{% for post in posts %}
<li class="post">
<div class="post-content">
#...
<div class="post-footer">
<a href="{{ url_for('main.post', id=post.id) }}"><span class="label label-default">Permalink</span></a>
</div>
</div>
</li>
{% endfor %}
</ul>
为每篇文章添加页脚添加一个链接, 指向视图函数post。