Skip to content

Instantly share code, notes, and snippets.

@pylemon
Created December 14, 2012 03:06
Show Gist options
  • Select an option

  • Save pylemon/4282379 to your computer and use it in GitHub Desktop.

Select an option

Save pylemon/4282379 to your computer and use it in GitHub Desktop.
django: html filter for clean up html tags
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.template.defaultfilters import stringfilter, striptags, slice_filter
def cut_string(value, limit):
s = striptags(value)
s = s.replace('\n', '').replace(' ', '')
return slice_filter(s, str(limit))
def test():
test_string = """
<html>
<title>测试一下</title>
<body>
<h1>Hello, world</h1>
<div class="test">
<li><span><a href="http://www.google.com" title="google">最后一句</a></span></li>
</div>
</body>
</html>
"""
print cut_string(test_string, 20)
if __name__ == '__main__':
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment