【问题标题】:Displaying HTML Source rather than rendering the code in GAE显示 HTML 源代码而不是在 GAE 中呈现代码
【发布时间】:2011-08-08 00:59:41
【问题描述】:

我正在尝试使用 Google App Engine 显示网站的源代码。这是我尝试过的代码的sn-ps。

 def post(self):
    s = 'http://www.bing.com'
    fp = urllib.urlopen(s)
    src = fp.read()
    self.response.out.write(template.render('main.html',{'temp1':src}))

我使用的模板有一些类似于

<div style="width:100%;height:100%">{{temp1}}</div>

这里的问题是它呈现源并给我网站的 html 页面,而不是我试图嵌入的代码。告诉我如何解决这个问题?

【问题讨论】:

    标签: python google-app-engine django-templates


    【解决方案1】:

    如果您想查看 html 而不是页面,则需要转义 html 实体。
    试试看:src = cgi.escape(src)
    也可以将其放在&lt;pre&gt;&lt;/pre&gt; 标签内以进行独特展示。

    【讨论】:

      【解决方案2】:

      (假设你使用的是 Appengine 自带的 Django 模板)你可以使用built-in escape filter,比如:

      <div style="width:100%;height:100%">{{temp1|escape}}</div>
      

      【讨论】:

        【解决方案3】:

        您可以将 HTML 代码放在 div 内的 &lt;pre&gt; 标记内。它用于预格式化的内容。也就是说,您希望浏览器完全按照您编写的方式显示它,因此它会忽略标签并且不会截断多个空格/制表符。

        <div style="width:100%;height:100%"><pre>{{temp1}}</pre></div>
        

        【讨论】:

        • pre 标签不会转义或忽略标签,只是其中的一部分。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-24
        • 1970-01-01
        • 2022-12-28
        • 2012-09-11
        • 2018-11-29
        • 2014-08-30
        • 2012-10-02
        相关资源
        最近更新 更多