Note: web.py any variables used will be escaped, so when you name set the value to be a piece of HTML, it will be escaped and displayed as plain text. If you want to turn off this option, you can write it $:name instead $name .
What if we want to move it partially?
Webpy obviously provides a transfer function that we can call directly on the application layer.
From web.net import htmlquote
Htmlquote (Raw_text)
-------------------------------
- #/usr/bin/python
- #coding =utf-8
- Import htmlparser
- Import sys
- Reload(sys)
- SYS. Setdefaultencoding(' utf-8 ')
- Html_parser = htmlparser. Htmlparser()
- Title = ' Eclipse features <template> learning. E.g: quickly insert timestamp in code-segmentfault '
- Newtitle = html_parser. Unescape(title)
- Print Newtitle
- You need to load the SYS module, reset the default encoding to UTF8, and there will be no error. However, the content to be processed is only the title part of an article, and the usual HTML escape content is as follows
It was decided to implement a simple escape function using Python's replace function, as follows:
- #/usr/bin/python
- #coding =utf-8
- def replace_html(s):
- s = s. Replace(' " ' ,' "')
- s = s. Replace(' & ' ,' & ')
- s = s. Replace(' < ' ,' < ')
- s = s. Replace(' > ' ,' > ')
- s = s. Replace(' ' ,")
- s = s. Replace('-361way.com ',')
- print s
- Replace_html(title)
The advantage is that it is fast and concise, does not depend on the module, and does not need to specify the default encoding reload SYS module.
Questions about automatic HTML escaping for webpy templates