django - 如何在 django 模板中删除 nbsp 并添加空间
问题描述
我需要在 django 模板中打印一个上下文变量....我的上下文变量名称“内容”
但是这个变量包含一些 html 标签和 ... 这是我的 html
<div style="max-width:800px;margin:50px auto;padding:0 12px">
<div class="m_-1931231161305542174card" style="background:white;border-radius:0.5rem;padding:2rem;margin-bottom:1rem">
{{ content }}
</div>
</div>
例如我的用户正在输入这个
Hello,
How are you?
该上下文变量包含
<p>Hello,</p> <p> </p> <p>How are you??</p>
为了删除 html 标签,我返回了一个函数
我试过这个方法
def remove_html_tags(text):
"""Remove html tags from a string"""
logger.info(text)
clean = re.compile('<.*?>')
return re.sub(clean, '', text)
def passing_contect(self):
-----------------
----------------------
-------------------)
context{'content':remove_html_tags(content)}//passing context without html tags
但 o/p 是
Hello, How are you ????
它仍然有这个 问题
如何处理
解决方案
在你的函数中试试这个:
def remove_html_tags(text):
"""Remove html tags and   from a string"""
logger.info(text)
clean = re.compile('<.*?>')
sans_tags = re.sub(clean, '', text)
return sans_tags.replace(' ', ' ')
这将用实际空间替换空间。
推荐阅读
- php - 数据迁移工具问题(magento 1.9 到 magento 2.3)
- mysql - INSERT INTO 的 sql 在一个条件下分支到两个表?
- c# - 指向 Google 搜索的链接在 IE 中引发错误请求 (400)
- javascript - 如何合理化代码以便为 openlayers 代码添加样式
- pycharm - Windows10上的Pycharm调试odoo11引发错误,代码为-1073740940(0xC0000374)
- angular - 如何使用带有柏树的 ng5-slider 测试表单?
- c - 将数组分配给C中的指针
- excel - 从另一张纸上跳跳虎超链接
- amazon-web-services - 使用 Kubeadm 进行 Kubernetes 集群自动扩展
- python-3.x - cvxpy install 导致win 10机器中的pycharm出错。怎么修?