[英]Python/Flask: Inject HTML to page
我有一個頁面,它從用戶那里獲取輸入並將其存儲在服務器端字典內的 object 中。
class post:
def __init__(self, title, content, date, poster, desc):
self.title = title
self.content = content
self.date = date
self.poster = poster
self.desc = desc
posts = {}
...
if request.method == "POST":
# NOTE: title and description is input, and content is textarea.
# I only want to input html in content textarea.
title = request.form["title"]
content = request.form["content"]
description = request.form["description"]
if title in posts:
flash("A post with that title already exists!")
return redirect(url_for("post_content"))
else:
posts[title] = post(title, content, date, session["user"], description)
flash("Posted successfully!")
return redirect(url_for("home"))
else:
return render_template("post.html")
...
然后我將帖子中的數據發送到另一個頁面
@app.route("/post/<title>")
def post_page(title):
data = posts[title]
return render_template("content.html", post=data)
這是內容的模板。html
{% extends "base.html "%}
{% block title %}{{post.title}}{% endblock %}
{% block content %}
<h1>{{post.title}}</h1>
<p>By {{post.poster}}, {{post.date}}</p>
<p><i>{{post.desc}}</i></p> <br>
<p>{{post.content}}</p>
{% endblock %}
但是,當我嘗試發送 HTML 時,它只是顯示為輸入(<h1>Hello</h1> 只是顯示它而不是使其成為標題)。 此外,如果我嘗試輸入多行,它只會在 output 中顯示為一長行。
如何將輸入的文本顯示為 HTML?
出於安全原因,您的 html 會被 jinja 自動轉義。 將 {% autoescape %} 設置為 false 或 {{ something | 安全 }} 除非您是 escaping 是您自己創建的或您信任的人,否則您可能會將自己和用戶置於 XSS 等攻擊之下。 您需要的是所見即所得的工具,例如 Quill、Summernote、ckeditor 或您選擇的任何產品,並使用漂白劑 package 等清潔和消毒 ZFC35FDC70D5FC69D23EZC 的任何部分。
我用所見即所得的編輯器 Quill 創建了一個 Flask-Blog,在后端使用漂白劑 package 進行消毒。 如果您想了解它們是如何實現的,請參閱此Flask-Blog
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.