第十一章 博客文章(五)

一. 博客文章的固定链接

用户有时希望能在社交网站和用户分享某篇文章的链接,为此每篇文章都要有一个唯一的URL引用。此处我们使用插入数据库时使用的唯一URL构建博客文章的URL:

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])

注意: 此处传给模板post.html的参数为一个列表,因为我们想和index.html、user.html共用博客的渲染模板_post.html。

因此固定链接需要添加到_post.html模板中,并显示在文章的下方:

app/templates/_posts.html:添加文章的固定链接

<div class="post-content">
    ...    
    <div class="post-footer">
        <a href="{{ url_for('.post', id=post.id) }}">
            <span class="label label-default">Permalink</span>
        </a>
    </div>
    ...

渲染固定链接页面的post.html模板

app/templates/post.html:固定链接模板

{% extends "base.html" %}
{% import "_macros.html" as macros %}

{% block title %}Flasky - Post{% endblock %}

{% block page_content %}
{% include '_posts.html' %}
{% endblock %}

点击Permalink链接进入文章详情页:

 二. 博客文章的编辑链接

与博客文章相关的最后一个功能是:博客文章编辑器。博客文章编辑器显示在单独的页面中,在这个页面的上部会显示文章的当前版本,下面跟着一个MarkDown编辑器,用于修改MarkDown源。这个编辑器基于Flask-PageDown实现,所以页面下部还会显示一个编辑后的文章预览。

app/templates/edit_post.html:编辑博客文章的模板

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block title %}Flasky - Edit Post{% endblock %}

{% block page_content %}
<div class="page-header">
    <h1>Edit Post</h1>
</div>
<div>
    {{ wtf.quick_form(form) }}
</div>
{% endblock %}

{% block scripts %}
{{ super() }}
{{ pagedown.include_pagedown() }}
{% endblock %}

app/main/views.py:编辑博客文章的路由

@main.route('/edit/<int:id>', methods=['GET', 'POST'])
@login_required
def edit(id):
    post = Post.query.get_or_404(id)
    if current_user != post.author and \
            not current_user.can(Permission.ADMIN):
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.body = form.body.data
        db.session.add(post)
        db.session.commit()
        flash('The post has been updated.')
        return redirect(url_for('.post', id=post.id))
    form.body.data = post.body
    return render_template('edit_post.html', form=form)

这个视图函数只允许博客文章和管理员编辑文章,如果试图编辑其它用户的文章,视图函数会返回403错误。这里使用的PostForm表单类和首页使用的是同一个表单。

最后,我们在每篇博客文章的下面添加一个指向编辑页面的链接:

app/templates/_posts.html:编辑博客文章的链接

...
        <div class="post-content">
            ...
            <div class="post-footer">
                {% if current_user == post.author %}
                <a href="{{ url_for('.edit', id=post.id) }}">
                    <span class="label label-primary">Edit</span>
                </a>
                {% elif current_user.is_administrator() %}
                <a href="{{ url_for('.edit', id=post.id) }}">
                    <span class="label label-danger">Edit [Admin]</span>
                </a>
                {% endif %}
                <a href="{{ url_for('.post', id=post.id) }}">
                    <span class="label label-default">Permalink</span>
                </a>
            </div>
        </div>
...

普通用户显示的编辑链接和管理员的编辑链接在样式上略有不同,以在视觉上表名这是管理功能。

点击编辑链接,进入文章编辑界面:

提交修改后,将重定向到文章详情页:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值