Django HTML截断 [英] Django HTML truncation

查看:141
本文介绍了Django HTML截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用内置 truncatewords_html 过滤Django,最后添加...,相反,我想用查看更多链接替换。

I'm using the build-in truncatewords_html filter of Django and it adds "..." in the end, instead, I want to replace this with a link "See More".

如何实现这一点?

推荐答案

最好编写自己的过滤器。您可以使用truncatewords_html的源代码,并将其用作过滤器的模板。它应该需要几个更改来获得你想要的,然后你只需要注册你的模板,并确保你加载到你想使用它的页面,你应该是好的。

It would be best to write your own filter. You could take the source code for truncatewords_html and use it as a template for your filter. It should take a few changes to get what you want, and then you will just need to register your template and make sure you load it on the page you want to use it on and you should be good.

查看此页面了解更多信息
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

See this page for more info https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

https://code.djangoproject.com/browser/django/trunk/django/ template / defaultfilters.py#L288

您应该可以复制方法,只需将代码更改为此。

You should be able to copy the method and just change the Code to this.

return Truncator(value).words(length,html = True,truncate ='see more')

你想让'看到更多'一个链接,这将需要更多的代码。我会更改过滤器以接受另一个参数,这是查看更多的链接。

You want to make 'see more' a link, that will take more code. I would change the filter to accept another param which is the link for 'see more'.

然后,而不是仅仅将'看到更多'传递给Truncator, HTML链接。

Then instead of just having 'see more' passed to Truncator you would pass the HTML link.

这篇关于Django HTML截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆