【问题标题】:Show content (include video and image) in Flask - Python在 Flask - Python 中显示内容(包括视频和图像)
【发布时间】:2021-05-09 14:00:54
【问题描述】:

该网站能够显示博客的全部内容,但不能显示视频内容。当我尝试发布包含视频的新帖子时。它只是显示 html 脚本。反正有没有从python传递整个html?

我正在使用 post.content 将帖子内容传递到 html 页面。它适用于文本,但不适用于图像或视频。

home.html:

<div class="media-body">
    <div class="article-metadata">
        <a class="mr-2" href="{{ url_for('users.user_posts', username=post.author.username)}}">{{ post.author.username }}</a>
        <small class="text-muted">{{ post.date_posted.strftime('%Y-%m-%d') }}</small>
    </div>
    <h2><a class="article-title" href="{{ url_for('posts.post',post_id=post.id) }}">{{ post.title }}</a></h2>
    <div class="article-content"><{{ post.content }}></div>
</div>

Routes.py

def home():
   page = request.args.get('page', 1, type=int)
   posts = Post.query.order_by(Post.date_posted.desc()) \
    .paginate(page=page, per_page=5)
   return render_template('home.html', posts=posts)

sample image

【问题讨论】:

  • 内容呈现为文本以防止Javascript注入攻击。

标签: python-3.x flask


【解决方案1】:

我刚刚找到了答案。就用这个吧:

{{ post.content|safe }}

像魅力一样工作。

参考:passing string with html tags to html from python

【讨论】:

    猜你喜欢
    • 2013-03-24
    • 2011-10-29
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多