Domanda

I'm making a blog in Python with Jinja2 as my templating engine. In my blog, there is a message box for a tittle and another one for the message. In your message, you can hashtag certain words. I managed to get it so that when the user submits a message with a hashtag, the word that contains the hashtag becomes a link.

if title and message:
    for word in message.split():
        if word[0] == "#":
            message =message.replace(word, "<a href="+word+">%s</a>" % word)
    m = Message(title = title, message = message)
    m.put()                  

The problem is that to allow the HTML to appear as a link and not HTML, I had to disable autoescaping in Jinja2 even though I want it. Is there anyone to make this code in Jinja2 instead of Python, this was autoescape will still be enabled.

È stato utile?

Soluzione

The message you are passing will look like

{{ blog.message|e }}

So content with links will look like

"This is a link"

and Not

"This is < a href="http://www.example.com/">a link"

Documentation here. http://jinja.pocoo.org/docs/templates/#html-escaping

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top