In Pyramid, how do I return raw HTML from a view?

前端 未结 2 1285
孤独总比滥情好
孤独总比滥情好 2021-01-22 20:25

I\'m really new to Pyramid (and pretty new to web frameworks in general).

I\'m trying to get to the stage where I can return raw HTML from a view, so that I can markup d

2条回答
  •  一生所求
    2021-01-22 21:09

    The string is being escaped, which is the default for strings inserted into templates to prevent naughty code being injected into your site. To mark the string as safe, you'll want to mark it as a literal so it's not escaped. I believe that pyramid (like pylons) ships with the webhelpers module, so you can import the literal function:

    from webhelpers.html import literal
    

    then replace your final results assignment with:

    results = literal("
      %s
    " %results)

    If literal doesn't ship with pyramid as I suspect, see this post: Python Pyramid & Chameleon templating language escapes html

    edit: note that you should probably be escaping your data from your db before you input it into the html for safety's sake. You can use cgi.escape for this.

提交回复
热议问题