我正在使用内置截断words_html https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#truncatewords-htmlDjango 的过滤器,它在最后添加了“...”,相反,我想用链接“查看更多”替换它。
我怎样才能实现这个目标?
最好编写自己的过滤器。您可以获取 truncatewords_html 的源代码并将其用作过滤器的模板。应该需要进行一些更改才能获得您想要的内容,然后您只需要注册您的模板并确保将其加载到您想要使用它的页面上,您应该就可以了。
请参阅此页面了解更多信息https://docs.djangoproject.com/en/dev/howto/custom-template-tags/ https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
https://code.djangoproject.com/browser/django/trunk/django/template/defaultfilters.py#L288 https://code.djangoproject.com/browser/django/trunk/django/template/defaultfilters.py#L288
您应该能够复制该方法并将代码更改为此。
return Truncator(value).words(length, html=True, truncate=' see more')
您想要将“查看更多”设为链接,这将需要更多代码。我会更改过滤器以接受另一个参数,即“查看更多”的链接。
然后,您将传递 HTML 链接,而不是仅仅将“查看更多”传递给 Truncator。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)