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

前端 未结 2 1284
孤独总比滥情好
孤独总比滥情好 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 20:55

    To prevent Chameleon from escaping the ${result} variable, you need to use ${structure: result}, as per the documentation: http://chameleon.readthedocs.org/en/latest/reference.html#structure

    0 讨论(0)
  • 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("<ul> %s </ul>" %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.

    0 讨论(0)
提交回复
热议问题