Cor*_*ode 3 html python django
我有一个系统从数据库中提取HTML内容,如下所示:
<p>This is some text</p>
<p>More text</p>
<p>Third Paragraph</p>
Run Code Online (Sandbox Code Playgroud)
当显示它时,它会显示博客文章内容的简短摘要,但是现在在显示文本时,我已将字符数限制设置为200这样的数字,这将导致摘要如下所示:
<p>This is some text</p>
<p>More text</
Run Code Online (Sandbox Code Playgroud)
切断任何地方的内容,可能会破坏网页的其余部分.有没有办法在python中关闭所有打开的标签或只获得一定数量的顶级块?例如,2个块将是前两个<p></p>标签.在显示文章的简短预览时,我看到word press具有此功能.
如果您需要在模板中执行此操作,请查看truncatewords-html:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#truncatewords-html
或者在其他地方你可以使用适用于大多数用例的底层django.utils.text.Truncator类(https://github.com/django/django/blob/1.7/django/utils/text.py#L65),如果你需要一些自定义行为使用该代码作为自己实现它的参考.
更新:
如果你需要django.utils.text.Truncator直接使用,这就是你实例化和调用words()方法的方法(这正是truncatewords-html过滤器的作用)
Truncator(value).words(length, html=True, truncate=' ...')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
801 次 |
| 最近记录: |