首页 > 解决方案 > 在浏览器上呈现 HTML 表单接收到的数据

问题描述

如何在接受输入时删除 HTML 表单显示的 (b' & \n),将其发送到 Python 脚本并获取要显示的输出:

<p id= "in">Please Do:<br />
<br />
1) Verify the serial.<br />
2) Input the serial.<br />
<form action="input" method="post">
{% csrf_token %}
Serial Number:
<input type="text" name="param" required>
<input type="submit" value="Submit"><br />
<br />
{{data_input}}
{{data1|safe}}
<br />
<br />
</form>
</p>

显示的输出:

b'xxx 成功添加到数据库...\n'

标签: html

解决方案


假设您的消息存储在一个变量中:

your_message = b'xxx Added Successfully To Database...\n'
formatted_message = your_message.replace(b'\n', b'')

然后输出formatted_message。在 html 中使用
标签时,我会在显示给用户的所有消息上进行这样的替换。

编辑:如果要将字节对象转换为普通字符串,可以执行以下操作:

formatted_message = formatted_message.decode()

推荐阅读