数据库存储了html格式的博客文章,在主页(index)显示的时候带有html标签,如何过滤掉呢?

 

解决方案

用jinja自定义filter过滤掉html标签

我是用的工厂函数,因此在工厂函数里面这样写

__init__.py
#省略。。。
def create_app():
    app = Flask(__name__)
    #省略。。。
    @app.template_filter('ellipsis')
    def do_ellipsis(arg):
        import re
        return re.sub(r"<.*?>",'',arg)
    #省略。。。
    return app
    
index.html
<p class="list-group-item-text blog-index-textbody">{{ post['body']|ellipsis  }}</p>

详见flask文档 - 模板

相关文章:

  • 2022-12-23
  • 2021-10-21
  • 2021-07-18
  • 2022-02-09
  • 2022-02-09
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2022-02-09
  • 2021-08-10
  • 2022-02-09
  • 2021-10-27
  • 2021-12-06
  • 2021-06-30
相关资源
相似解决方案