如何确保所有 html 标签都已关闭?出现问题是因为我想为每篇文章创建某种摘录。例如有人写了这样一篇文章:
Hi everyone, I'm just an article and I have few <strong>tags</strong> inside <em>of me</me>
如果我在“标签”之后剪切此消息,我会得到一个未关闭的标签。
在将用户输入的文本保存到数据库之前,如何使用 Django 检查所有用户的输入文本?
在 Django 1.7 中,有一个特定的模板过滤器,称为truncatechars_html https://docs.djangoproject.com/en/dev/ref/templates/builtins/#truncatechars-html:
与 truncatechars 类似,但它可以识别 HTML 标签。任何
在字符串中打开且在之前未关闭的标签
截断点在截断后立即关闭。
另请参阅truncatewords_html https://docs.djangoproject.com/en/dev/ref/templates/builtins/#truncatewords-html.
对于 Djangotruncatewords_html,或者根据 1.7 中实现的过滤器制作一个自定义过滤器(source https://github.com/django/django/blob/484f3edf1e79388f73dcb07e39d79d0c5029ae9e/django/template/defaultfilters.py#L276-L290),或者使用这个snippet https://djangosnippets.org/snippets/1477/(还没有测试过)。另请参阅与片段博客文章相关的内容:HTML 的安全截断 http://ole-laursen.blogspot.com/2009/05/safe-truncation-of-html.html.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)