寻找小费。
我有一个以其他格式生成的随机字符串:
string = """[TAG1] Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Maecenas commodo diam ac sollicitudin vestibulum. Nunc ac
dignissim elit.
[TAG2] Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Maecenas commodo diam ac sollicitudin vestibulum. Nunc ac
dignissim elit.
"""
现在,我想将其传递给html模板并保留其实际格式。
现在我将字符串用'\ n'和:
{% for line in comments %}
<div>{{ line|safe}}</div>
{% endfor %}
结果为:
string = """[TAG1] Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Maecenas commodo diam ac sollicitudin vestibulum. Nunc ac
dignissim elit.
[TAG2] Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Maecenas commodo diam ac sollicitudin vestibulum. Nunc ac
dignissim elit.
"""
我应该用html实体替换空格吗?可接受的方法是什么?
答案 0 :(得分:0)
两者都很好。.希望这对您有帮助
<p style="white-space: pre-wrap;">{{ string }}</p>
OR
<p style="white-space: pre-line;">{{ string }}</p>
更新
main.py
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
string = """[TAG1] Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Maecenas commodo diam ac sollicitudin vestibulum. Nunc ac
dignissim elit.
[TAG2] Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Maecenas commodo diam ac sollicitudin vestibulum. Nunc ac
dignissim elit.
"""
return render_template('index.html', string=string)
index.html
<html>
<head>
</head>
<body>
<p style="white-space: pre-line;">{{ string }}</p>
</body>
</html>