返回保留空间的烧瓶字符串

时间:2020-06-24 04:27:05

标签: python html flask

这是我的样品瓶代码,

from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/')
def root():
    report = ["str1       1\n","     str2 2"]
    report = "".join(report)
    report = report.replace('\n', '<br>')
    return render_template('detailed_report.html', report = report)
app.run(debug=True)

这是我对应的HTML代码,

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
<div>
    {% autoescape false %}
        {{report}}
    {% endautoescape %}
</div>
</body>
</html>

预期结果:

str1       1
     str2 2

实际结果:

str1 1
str2 2

我应该怎么做才能保留空间? 在此先感谢:)

1 个答案:

答案 0 :(得分:2)

要使间距有效,需要一点css!

.barcode{
    white-space:pre;
}
<div class="barcode">{% autoescape false %}{{report}}{% endautoescape %}</div>

这是一个例子

.barcode {
  white-space: pre;
  /* or pre-wrap if you want wrapping to still work.*/
}
<div class="barcode">HELlo world eg a    

nd i am a            good go </div>