开发者

Displaying HTML Source rather than rendering the code in GAE

开发者 https://www.devze.com 2023-02-27 00:23 出处:网络
I am trying to display the source code of the site using Google App Engine. Here is the snippets of the code that I have tried out.

I am trying to display the source code of the site using Google App Engine. Here is the snippets of the code that I have tried out.

 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}))

And the template I have used have some thing similar to

<div style="width:100%;height:100%">{{temp1}}开发者_如何学JAVA</div>

The problem here is that it renders the source and gives me the html page of the site rather than the code which I am trying to embed. Let me know how to get this solved?


If you want to see the html and not the page you gonna need to escape the html entities.
Try: src = cgi.escape(src)
And maybe put it inside <pre></pre> tags for distinctive displaying.


(assuming that you're using the Django templates that come with Appengine,) you can use the built-in escape filter, like:

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


You could put the HTML code inside the div inside a <pre> tag. It's for pre-formatted content. That is, you want the browser to display it exactly the way you write it, so it ignores tags and does not truncate multiple spaces/tabs.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号