【问题标题】:passing html element in variable in app engine?在应用引擎的变量中传递html元素?
【发布时间】:2015-03-15 08:35:53
【问题描述】:

我在应用引擎的 ndb 数据存储区中将文本保存在 markdown 中。在检索时,我正在使用 markdown2 库将 Markdown 转换为 html 并将其作为参数传递给模板进行渲染。代码如下:

article_content = model.Article.query(model.Article.tittle == article_tittle).fetch()

if article_content:
        for article in article_content:
        content =  markdown.markdown(article.content)
        tittle = article.tittle
        date = article.date

        params = {
            'page' : 'article',
            'tittle' : tittle,
            'content' : content
        }

        self.render_response('article.html',**params)

但是当我渲染模板时,html标签被转换为html实体:

<article class="article"><p>A minimalist writing zone, where you can block out all distractions and get to what's important. The writing!bvbvbvjbvbknvbvbnvbvbn jvnbnvbgnb n vgnvbn n n bn inovblv  o  bgv vomhg bmvm vmbvl lm   mvjpbg</p><p>Use <strong>bold</strong>, <em>italics</em>, <strong><em>both</em></strong> <strong>and</strong> <a href="http://zenpen.io"> urls </a>_ just_ by highlighting the text and selecting them from the tiny options box that appears above it. </p><p>fdbgbfbvbvbbghh</p><blockquote>  <p>Quotes are easy to add too!  fjbgbknvkbvnm i m vm vi mfbm b bo gb gbgb  gjkcdfkvc bnbv n oibio nbiffhi nfjkgfkjv  gdrf</p></blockquote><p>gdfbcdjifjgfgethfb</p><p>gfbfgbf</p><p>hhgbvgfgh</p></article>

如果我将变量作为 json 转储或简单地通过 self.response.write() 传递,则它是正确传递的。

有没有办法将变量中的html元素传递给模板。我正在使用 jinja2 进行模板化。 另外,如果可以,请描述原因以及应用引擎如何将变量传递给模板.thanx

【问题讨论】:

  • 你能在 jinja2 中将变量标记为安全吗? {{ content|safe }}
  • 哦,我完全逃脱了,..谢谢!!

标签: javascript python html google-app-engine jinja2


【解决方案1】:

您正在与 autoescaping 发生冲突,请参阅 http://jinja.pocoo.org/docs/dev/api/#autoescaping 。正如@dm03514 在评论中提到的那样,最好的解决方案是 关闭自动转义(它可能会使您面临“注入”攻击),而是使用safe Jinjia2 特别标记为安全内置过滤器,您知道的 HTML 安全的并且想要这样注入。

【讨论】:

  • 谢谢.. :) 我错过了。
  • @VikashKumar,不客气——如果这有帮助,记得接受答案(点击答案左侧的复选标记大纲)——就是这样StackOverflow 工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 2013-10-31
  • 2018-10-18
相关资源
最近更新 更多