2011-04-07 18 views

回答

3

取決於過濾器。在某些情況下,您可以導入包含過濾器的模塊並訪問模塊內的幫助器功能,但在其他情況下,您不會那麼幸運。有關詳細信息,請參閱過濾源。

+0

臨時nk你,過濾器實際上是使用另一個django實用程序方法,我可以導入 – MattoTodd 2011-04-07 17:46:51

+0

https://stackoverflow.com/questions/17226779/django-humanize-outside-of-template – 2018-01-18 09:11:25

24

一般來說,是的。例如,如果你的過濾器是在django.template.defaultfilters你可以運行:

from django.template.defaultfilters import slugify 
slugify('what is that smell') 
0

而是在模板使用這個的:

{{text | linebreaks}} 

我用這個來實現斷行過濾器:

from django.template.defaultfilters import linebreaks 
text = "some text \n next line" 
text = linebreaks(text) 

給出:

<p>some text <br /> new line</p>