当前位置:网站首页>Flask blog practice - realize the latest articles and search in the sidebar

Flask blog practice - realize the latest articles and search in the sidebar

2022-06-25 10:14:00 Light programming

Implement the latest article module

This is very simple , Just send the latest article into blog In the context of

stay app/blog/views.py Of inject_archive The new code in the function is as follows :

@bp.context_processor
def inject_archive():
    #  Article archive date injection context 
    posts = Post.query.order_by(Post.add_date)
    dates = set([post.add_date.strftime("%Y year %m month ") for post in posts])

    #  label 
    tags = Tag.query.all()
    for tag in tags:
        tag.style = ['is-success', 'is-danger', 'is-black', 'is-light', 'is-primary', 'is-link', 'is-info', 'is-warning']

    #  The latest article 
    new_posts = posts.limit(6)
    return dict(dates=dates, tags=tags, new_posts=new_posts)

stay app/blog/templates/cate_list.html Add the following code to the sidebar of :

<div class="box is-shadow
原网站

版权声明
本文为[Light programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250918142234.html