2011-11-19 35 views
0

你好,我应该如何逃避链接渲染?如何逃避或不隐藏这个序列?

enter image description here

我现在写它的方式是过滤器: {{article.text|striptags|urlize|nl2br|safe}}

你能推荐该怎么办呢?

相关问题:https://stackoverflow.com/questions/8179801/autolinebreaks-filter-in-jinja2

谢谢

+1

好吧,如果你想HTML标签被解释为HTML标签,根本不要转义它。 –

+0

,但'{{article.text | striptags | urlize | nl2br}}'和escapes之间的输出呈现切换没有区别? –

+1

[Python HTML sanitizer/scrubber/filter]的可能的重复(http://stackoverflow.com/questions/699468/python-html-sanitizer-scrubber-filter) –

回答

1

平时我想用HTMLParser进行处理(矫枉过正也许?),下面的示例代码为Python 2.7(3.0图书馆更名为html.parser

from HTMLParser import HTMLParser 

class MyHTMLParser(HTMLParser): 
    def handle_starttag(self, tag, attrs): 
     print "Found Start Tag", attrs 

s = "noivos, convites de casamento <a href=\"http://www.olharcaricato.com.br\"> 
     http://www.olharcaricato.com.br</a> more entries here" 

parser = MyHTMLParser() 
parser.feed(s) 

输出:Found Start Tag [('href', 'http://www.olharcaricato.com.br')]

注:实现上述代码作为过滤器,可以根据需要调整输出。过滤器的例子发现在Custom jinja2 filter for iterator

+0

很酷的解决方案!谢谢你,正如你所说,当我现在真正需要的时候,它可能是矫枉过正的,用'
'代替'\ n', –