Search code examples
pythonflaskmarkup

flask app not rendering html tags properly


I have a sentence

sentence =  <p> Reading, watching or <span class="matching">listening</span> to the media isn’t <span class="matching">matching</span><span class="matching">much</span> help either. </p>

to make it render properly at front-end here is what I have done

from flask import Markup
sentence = Markup(sentence)

But the output is only rendered properly for one markup (not necessarily first one) and others are not rendered.

            <p> Reading, watching or <span class="matching">listening</span> to the media isn’t &lt;span class=&#34;matching&#34;&gt;much&lt;/span&gt; help either. </p>

What am I doing it wrong here?


Solution

  • The culprit is the

    isn’t

    that "’" is not valid ASCII, because of which it doesn't come in the valid range of characters of HTML Markup , thus it escapes it (though it should throw an error)

    Hope that solves the issue.

    This works for me

    from flask import Markup
    sentence =  '<p> Reading, watching or <span class="matching">listening</span> to the media isn\'t <span class="matching">matching</span><span class="matching">much</span> help either. </p>'
    Markup(sentence)
    

    returns

    Markup(u'<p> Reading, watching or <span class="matching">listening</span> to the media isn\'t <span class="matching">matching</span><span class="matching">much</span> help either. </p>')
    

    hope that is what is the required output