python - 没有格式的 Python Flask 博客消息
问题描述
我有一个简单的消息博客的代码,可以留言。问题之一是来自 sqlite db 的消息显示没有格式,例如没有段落。我怎样才能改进它(或添加降价启用)?我感谢您的帮助。谢谢你。
主应用 py
@app.route('/')
def index():
conn = db_conn()
posts = conn.execute('SELECT * FROM table_posts').fetchall()
conn.close()
return render_template('index.html', posts=posts)
@app.route('/create_new_post', methods=('GET', 'POST'))
def create_new_post():
if request.method == 'POST':
content = request.form['content']
conn = db_conn()
conn.execute('INSERT INTO table_posts (content) VALUES (?)', (content,))
conn.commit()
conn.close()
return redirect(url_for('index'))
else:
return render_template('create_new_post.html')
索引.html
{% extends 'base.html' %}
{% block title %}
Simple Message Board
{% endblock %}
{% block content %}
{% for post in posts %}
<br>
<div class="card">
<div class="card-body">
<p class="card-text"> {{ post['content'] }} </p>
<span class="badge badge-secondary">{{ post['time_stamp'] }}</span>
</div>
</div>
{% endfor %}
{% endblock %}
我想要的结果如下:
Text of 1st line
Text of 2nd line
Text of 3rd line
但实际显示的内容如下:
Text of 1st line Text of 2nd line Text of 3rd line
解决方案
我认为您正在尝试将正文呈现为 html 而不是文本。
jinja 自动转义文本,您可以停止自动转义,为此您可以使用 jinja 的安全过滤器。
{{ post['content']|safe }}
您还可以查看文档
推荐阅读
- javascript - 产品项目的简单滑块
- javascript - 在Javascript中使用for循环创建列表的问题
- laravel - orderBy on hasMany 关系 laravel
- laravel - Laravel - 用户 > 角色 > 公司内的权限
- php - 并行发送请求 - 将更大的数据传递给异步工作函数
- android - 如何在 Android 的任何地方访问共享偏好数据
- sql - 如何仅通过 a 列中具有 b 列中所有值的 id 进行过滤
- bash - 进程替换从哪里获取标准输入
- octave - 我找不到如何解决这个解析错误
- python-3.x - 在 Anaconda 提示符下运行 bash 脚本