栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

是否有处理“…更多”的Django模板过滤器,当您单击它时,它会显示更多文本?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

是否有处理“…更多”的Django模板过滤器,当您单击它时,它会显示更多文本?

只是简单地说明了这一点,似乎可以完成您想做的事,并且不依赖任何外部JS库。

免责声明:我没有在IE中尝试过,但是chrome和firefox可以正常工作。

from django import templatefrom django.utils.html import escapefrom django.utils.safestring import mark_saferegister = template.Library()import rereadmore_showscript = ''.join(["this.parentNode.style.display='none';","this.parentNode.parentNode.getElementsByClassName('more')[0].style.display='inline';","return false;",]);@register.filterdef readmore(txt, showwords=15):    global readmore_showscript    words = re.split(r' ', escape(txt))    if len(words) <= showwords:        return txt    # wrap the more part    words.insert(showwords, '<span  >')    words.append('</span>')    # insert the readmore part    words.insert(showwords, '<span >... <a href="#" onclick="')    words.insert(showwords+1, readmore_showscript)    words.insert(showwords+2, '">read more</a>')    words.insert(showwords+3, '</span>')    # Wrap with <p>    words.insert(0, '<p>')    words.append('</p>')    return mark_safe(' '.join(words))readmore.is_safe = True

要使用它,只需在您的应用中创建一个templatetags文件夹,在其中创建

__init__.py
文件,然后将此代码放入即可
readmore.py

然后在要使用它的任何模板的顶部,只需添加:

{% load readmore %}

要使用过滤器本身:

{{ some_long_text_var|readmore:15 }}

:15告诉您在阅读更多链接之前要显示多少个单词。

如果您想像Ajax一样加载全部内容,那会有些不同,并且需要更多的基础架构。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669666.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号