FLASK开源项目--派生吧博客推介之五

数据库的部分已经介绍完成,现在介绍了最后一部分:视图函数。

对于博客而言,视图函数需要完成的是:首页,文章页,分类页,标签页,搜索页,错误页面,关于页面。


  • 错误页面

这个部分代码十分简单,直接返回404的模板。

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404


@app.route('/error/404')
def error_404():
    return render_template('404.html'), 404

  • 关于页面

直接返回about页面的模板,直接需要额外处理页面的侧边栏,这个时候需要传一些参数:所有的分类,热门文章,最新文章,随机便签,最新评论。

@app.route('/about')
def about():
    categorys = Category.query.getall()   #所有的分类
    hot = Post.query.hottest()[:20]       #热门文章,20个
    new = Post.query.newpost()[:20]       #最新文章,20个

    tag = Tag.query.getall()
    shuffle(tag)
    tag = tag[:20]                        #标签云,20个

    comments = Comment.query.newcomment()[:20]  #最新评论,20个

    return render_template('/about.html',
                           categorys=categorys,
                           hotarticles=hot,
                           newpost=new,
                           tags=tag,
                           comments=comments)

  • 首页

对于首页需要关注的是分页的情况,幸好,flask-sqlalchemy 提供了相应的处理函数,这里需要处理的提供当前页面,最后页数的参数到模板中。

@app.route('/')
@app.route('/page/')
def index(pageid=1):

    categorys = Category.query.getall()

    p = Post.query.getpost_perpage(pageid, per_page)
    hot = Post.query.hottest()[:20]
    new = Post.query.newpost()[:20]

    tag = Tag.query.getall()
    shuffle(tag)
    tag = tag[:20]

    comments = Comment.query.newcomment()[:20]
    articles = p.items
    if not p.total:
        pagination = [0]
    elif p.total % per_page:
        pagination = range(1, p.total / per_page + 2)
    else:
        pagination = range(1, p.total / per_page + 1)

    return render_template('/index.html',
                           categorys=categorys,
                           articles=articles,
                           hotarticles=hot,
                           newpost=new,
                           tags=tag,
                           comments=comments,
                           pageid=pageid,
                           pagination=pagination[pageid - 1:pageid + 10],
                           last_page=pagination[-1],
                           nav_current="index"
                           )

  • 标签,分类页面

这个其实跟首页雷同,只是需要提供的文章内容不同。代码就略了。


  • 搜索页面

对于搜索页面,我们需要获取请求的搜索关键词,然后调用数据库的查询函数,传入关键字就行。

@app.route('/search')
@app.route('/search/page/')
def search(pageid=1):

    categorys = Category.query.getall()
    hot = Post.query.hottest()[:20]
    new = Post.query.newpost()[:20]
    tag = Tag.query.getall()
    shuffle(tag)
    tag = tag[:20]
    comments = Comment.query.newcomment()[:20]

    searchword = request.args.get('s', '')
    if not searchword:
        return redirect(url_for('error_404'))

    searchresult = Post.query.search(searchword)

    p = pageby(searchresult, pageid, per_page, Post.post_create_time.desc())

    articles = p.items
    if not p.total:
        pagination = [0]
    elif p.total % per_page:
        pagination = range(1, p.total / per_page + 2)
    else:
        pagination = range(1, p.total / per_page + 1)

    return render_template('/search.html',
                           key=searchword,
                           categorys=categorys,
                           articles=articles,
                           hotarticles=hot,
                           newpost=new,
                           tags=tag,
                           comments=comments,
                           pageid=pageid,
                           pagination=pagination[pageid - 1:pageid + 10],
                           last_page=pagination[-1]
                           )


  • 文章页

对于文章页面,只需要传入指定的文章内容就行了,侧边栏跟首页的处理方式一样。

@app.route('/article/')
def article(postid=5):
    categorys = Category.query.getall()
    hot = Post.query.hottest()[:20]
    new = Post.query.newpost()[:20]
    tag = Tag.query.getall()
    shuffle(tag)
    tag = tag[:20]
    comments = Comment.query.newcomment()[:20]
    articles = Post.query.getall()
    shuffle(articles)
    articles = articles[:5]

    post = Post.query.getpost_id(postid)

    if not post:
        return redirect(url_for('error_404'))
    postcoments = post.comments.all()
    db.engine.execute('update post set view_num = view_num + 1 where id =' +
                      str(postid))
    return render_template('/post.html',
                           post=post,
                           articles=articles,
                           categorys=categorys,
                           hotarticles=hot,
                           newpost=new,
                           tags=tag,
                           comments=comments,
                           postcoments=postcoments
                           )




最后特别需要强调的是,上线的代码与这些内容有些区别,因为侧边栏是很多页面需要共享的,所以实际线上的代码,这一部分的内容是缓存起来的,另外对首页,标签页,分类页,部分文章页面有10分钟的缓存。