如何使用FLASK将python字符串中的新行呈现为HTML模板? message=“” @app.route(“/”,methods=[“GET”,“POST”]) def upload_文件(): 全球信息 如果request.method==“POST”: 如果请求文件: 数据=请求.文件[“文件”] 如果data.filename==“”: message=“文件没有名称!” 允许的elif_文件(data.filename): 消息+=“允许使用文件” data.save(os.path.join(app.config[“文件上传”],data.filename)) 消息+=“已保存文件” 如果(验证()): 消息+=“文件已验证!” 其他:消息+=“验证失败” 其他: 消息+=“不允许使用文件扩展名!” 返回呈现模板(“ui.html”,message=message)

如何使用FLASK将python字符串中的新行呈现为HTML模板? message=“” @app.route(“/”,methods=[“GET”,“POST”]) def upload_文件(): 全球信息 如果request.method==“POST”: 如果请求文件: 数据=请求.文件[“文件”] 如果data.filename==“”: message=“文件没有名称!” 允许的elif_文件(data.filename): 消息+=“允许使用文件” data.save(os.path.join(app.config[“文件上传”],data.filename)) 消息+=“已保存文件” 如果(验证()): 消息+=“文件已验证!” 其他:消息+=“验证失败” 其他: 消息+=“不允许使用文件扩展名!” 返回呈现模板(“ui.html”,message=message),python,flask,Python,Flask,我正在尝试使用flask验证上传到ui.html模板上的文件,我想向ui.html发送一个关于验证状态的“消息”字符串,并将其很好地显示出来。每当一个新字符串添加到“消息”字符串时,我都会尝试添加新行,以便在ui.html中呈现它时,在我想要的地方添加了新行。 以下是我在ui.html中呈现“message”字符串的方式: message="" @app.route("/", methods=["GET", "POST"]) def upload_file(): global message i

我正在尝试使用flask验证上传到ui.html模板上的文件,我想向ui.html发送一个关于验证状态的“消息”字符串,并将其很好地显示出来。每当一个新字符串添加到“消息”字符串时,我都会尝试添加新行,以便在ui.html中呈现它时,在我想要的地方添加了新行。 以下是我在ui.html中呈现“message”字符串的方式:

message=""
@app.route("/", methods=["GET", "POST"])
def upload_file():
global message
if request.method == "POST":
    if request.files:
        data=request.files["file"]
        if data.filename == "":
            message="File doesn't have a name! <br>"
        elif allowed_file(data.filename):
            message+="File Allowed <br>"
            data.save(os.path.join(app.config["FILE_UPLOAD"], data.filename))
            message+="File Saved"
            if(validate()):
                message+="File validated! <br>"
            else: message+="Failed validation <br>"
        else:
            message+="File extension not allowed! <br>"
return render_template("ui.html",message=message)
{%if消息%}
{{message}}

{%endif%}
但是ui.html并没有呈现

,而是在ui.html模板上以字符串形式打印。我如何解决这个问题?我也尝试过

中也提到过 flask的模板引擎(jinja2)假设{{}}内部的输入不安全,不允许在其中呈现js或html。最简单的方法就是使用安全的过滤器来做这样的事情

{% if message %}
     <p>{{ message }}</p>
{% endif %}
根据flask的文档,还有另外两种控制自动转义行为的方法,即将HTML字符串包装成禁用自动转义,如下所示:

{{ message | safe }}
{%autoescape false%}
此处禁用自动转义
{{你不会逃脱吗}
{%endautoescape%}
中也提到了 flask的模板引擎(jinja2)假设{{}}内部的输入不安全,不允许在其中呈现js或html。最简单的方法就是使用安全的过滤器来做这样的事情

{% if message %}
     <p>{{ message }}</p>
{% endif %}
根据flask的文档,还有另外两种控制自动转义行为的方法,即将HTML字符串包装成禁用自动转义,如下所示:

{{ message | safe }}
{%autoescape false%}
此处禁用自动转义
{{你不会逃脱吗}
{%endautoescape%}

我用烧瓶提供的闪光功能解决了这个问题。它单独打印每条消息,因此我只能在HTML文件中添加
。 在ui.html文件中进行的渲染更改包括:

{% autoescape false %}
<p>autoescaping is disabled here
<p>{{ will_not_be_escaped }}
{% endautoescape %}
{%for get_flashed_messages()中的消息%}
{{message}}

{%endfor%}
我用烧瓶提供的闪光功能解决了这个问题。它单独打印每条消息,因此我只能在HTML文件中添加
。 在ui.html文件中进行的渲染更改包括:

{% autoescape false %}
<p>autoescaping is disabled here
<p>{{ will_not_be_escaped }}
{% endautoescape %}
{%for get_flashed_messages()中的消息%}
{{message}}

{%endfor%}
很高兴知道这一点,尽管我用flask提供的flash功能解决了这个问题。它单独打印每条消息,这样我就可以只在HTML文件中添加。很高兴知道这一点,尽管我用flask提供的flash功能解决了这个问题。它单独打印每条消息,这样我就可以只在HTML文件中添加。