Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否有一个Django模板过滤器来处理“;。。。“更多”;当你点击它,它会显示更多的文字?_Python_Django_Templates_Plugins_Filter - Fatal编程技术网

Python 是否有一个Django模板过滤器来处理“;。。。“更多”;当你点击它,它会显示更多的文字?

Python 是否有一个Django模板过滤器来处理“;。。。“更多”;当你点击它,它会显示更多的文字?,python,django,templates,plugins,filter,Python,Django,Templates,Plugins,Filter,假设我有一个很大的段落 我只想展示前15个单词。之后,用户单击“更多”以查看其余内容。刚刚启动了此功能,似乎可以执行您想要的操作,并且不依赖任何外部JS库 免责声明:我还没有在IE中尝试过,但chrome和firefox都很好用 from django import template from django.utils.html import escape from django.utils.safestring import mark_safe register = template.Lib

假设我有一个很大的段落


我只想展示前15个单词。之后,用户单击“更多”以查看其余内容。

刚刚启动了此功能,似乎可以执行您想要的操作,并且不依赖任何外部JS库

免责声明:我还没有在IE中尝试过,但chrome和firefox都很好用

from django import template
from django.utils.html import escape
from django.utils.safestring import mark_safe

register = template.Library()

import re

readmore_showscript = ''.join([
"this.parentNode.style.display='none';",
"this.parentNode.parentNode.getElementsByClassName('more')[0].style.display='inline';",
"return false;",
]);

@register.filter
def 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 class="more" style="display:none;">')
    words.append('</span>')

    # insert the readmore part
    words.insert(showwords, '<span class="readmore">... <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
来自django导入模板的

从django.utils.html导入转义
从django.utils.safestring导入标记_safe
register=template.Library()
进口稀土
readmore\u showscript=''。加入([
“this.parentNode.style.display='none';”,
“this.parentNode.parentNode.getElementsByClassName('more')[0].style.display='inline';”,
“返回false;”,
]);
@寄存器过滤器
def readmore(txt,showwords=15):
全局readmore\u showscript
words=re.split(r'',转义(txt))
如果len(words)有过滤器,尽管您仍然需要一个JavaScript助手来完成所描述的操作。

来自django导入模板
from django import template
from django.utils.html import escape
from django.utils.safestring import mark_safe

register = template.Library()


@register.filter
def readmore(text, cnt=250):
    text, cnt = escape(text), int(cnt)

    if len(text) > cnt:
        first_part = text[:cnt]
        link = u'<a href="javascript:;" class="more">%s</a>' % _('read more')
        second_part = u'%s<span class="hide">%s</span>' % (link, text[cnt:])
        return mark_safe('... '.join([first_part, second_part]))
    return text

readmore.is_safe = True
从django.utils.html导入转义 从django.utils.safestring导入标记_safe register=template.Library() @寄存器过滤器 def readmore(文本,cnt=250): text,cnt=转义(text),int(cnt) 如果len(text)>cnt: 第一部分=文本[:cnt] 链接=u'%\u('阅读更多') 第二部分=u'%s%s'(链接,文本[cnt:]) 返回标记为安全(“…”。连接([第一部分,第二部分]) 返回文本 readmore.is_safe=True
使用truncatechars\u html

参考:

truncatechars\u html
与truncatechars类似,只是它知道HTML标记。在字符串中打开但在截断点之前未关闭的任何标记都将在截断后立即关闭。
例如:
{{value}truncatechars_html:9}
如果值为“Joel是一个slug

”,则输出将为“Joel i..

”。 HTML内容中的换行符将被保留。
我重写了前面的一个答案,以便更清楚地处理字符串转义:

@register.filter(needs_autoescape=True)
@stringfilter
def read_more(s, show_words, autoescape=True):
    """Split text after so many words, inserting a "more" link at the end.

    Relies on JavaScript to react to the link being clicked and on classes
    found in Bootstrap to hide elements.
    """
    show_words = int(show_words)
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    words = esc(s).split()

    if len(words) <= show_words:
        return s

    insertion = (
        # The see more link...
        '<span class="read-more">&hellip;'
        '    <a href="#">'
        '        <i class="fa fa-plus-square gray" title="Show All"></i>'
        '    </a>'
        '</span>'
        # The call to hide the rest...
        '<span class="more hidden">'
    )

    # wrap the more part
    words.insert(show_words, insertion)
    words.append('</span>')
    return mark_safe(' '.join(words))

我不知道有任何这样的过滤器,但它应该不会那么难写。你想用JavaScript显示更多的信息,对吗?我认为这很接近。在你开始插入舞蹈之前,如果len(words)>showwords(尽管仍然需要标记),你只是缺少了
,否则你会在不需要的时候添加readmore链接。哦,是的,我知道:)我会通过缩进more和readmore部分来确保标记插入和标记的安全性。谢谢,这是一个很好的解决方案!Thx 4这篇很棒的帖子:D@rossipedia,您好,我面临此自定义筛选器的问题。它似乎不适用于内置过滤器。例如,{post.text | safe | linebreaksbr | readmore:15}添加了“readmore”链接,过滤器工作正常,但其他内置过滤器,如“safe”和“linebreaksbr”现在不工作。你能帮忙吗?还有其他人面临过这个问题吗?@SkGolamSaroar对不起,这个答案很老了,我已经八年多没有和Django合作了。恐怕我目前不知道如何调试。这是更好的更新答案,即使您应该更改truncatewords_html的答案。这并不能解决问题(即显示<代码>更多链接)。非常好!您还需要:
从django.template.defaultfilters导入stringfilter
从django.utils.html导入条件_escape
。使用Django3.0
@register.filter(needs_autoescape=True)
@stringfilter
def read_more(s, show_words, autoescape=True):
    """Split text after so many words, inserting a "more" link at the end.

    Relies on JavaScript to react to the link being clicked and on classes
    found in Bootstrap to hide elements.
    """
    show_words = int(show_words)
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    words = esc(s).split()

    if len(words) <= show_words:
        return s

    insertion = (
        # The see more link...
        '<span class="read-more">&hellip;'
        '    <a href="#">'
        '        <i class="fa fa-plus-square gray" title="Show All"></i>'
        '    </a>'
        '</span>'
        # The call to hide the rest...
        '<span class="more hidden">'
    )

    # wrap the more part
    words.insert(show_words, insertion)
    words.append('</span>')
    return mark_safe(' '.join(words))
$(".read-more").click(function(e) {
    e.preventDefault();
    var t = $(this);
    t.parent().find('.more').removeClass('hidden');
    t.addClass('hidden');
});