Search code examples
pythonhtmlcharacter-encodingescapingtornado

apostrophe " ' " retrieved as ' in tornado-python template


I have a record in database [Maria Db]that contains an apostrophe ',when i retrieve the data from database the apostrophe become &#39 which is a HTML Character Reference; am using Python and tornado as a framework to generate data in template My Question is how to prevent apostrophe from becoming &#39 ?

Note : when i retrieve the list from database and print it there is no &#39 the conversion happens when i generate values in tornado template so the problem is not from the database.

1 - retrieving the list
(nb_, results_) = await BddInterface.execute("SELECT switches ... )

2 - append results in a list :

tmp_S_array.append(tmp_s)
tmp_S_array.append(tmp_n)
tmp_S_array.append(tmp_a)

3- render the list :

   self.render(template.html,items=tmp_S_array)

4- In the template

        <option value="" SELECTED></option>
            {% for item1 in items %}
               <optgroup label="{{ escape(item1[0]) }}">
                {% for item2 in item1[1] %}
                   <option value="{{ escape(item2) }}">{{escape(item2)}}</option>
             {% end %}
             {% end %}

in the logs when i log the list apostrophe is there
Info Information Switches =["Cote d'Azur", '##',... ]
in the option in the template
it shows like this "d&#39Azur"


Solution

  • The apostrophe or single-quote character is being escaped by the escape function in the template.

    When you're processing the value after form submission, you can unescape it the handler like this:

    from tornado.escaping import xhtml_unescape
    
    class MyHandler(web.RequestHandler):
        def post(self):
            value = xhtml_unescape(submitted_value_in_the_form)
    
            ...