Try to use cgi.escape to avoid manually escaping everything.