使用 Python/GAE 动态生成 HTML div/JS 脚本
Posted
技术标签:
【中文标题】使用 Python/GAE 动态生成 HTML div/JS 脚本【英文标题】:dynamically generating HTML divs/JS scripts with Python/GAE 【发布时间】:2013-05-03 07:53:08 【问题描述】:我正在构建一个 web 应用程序,我希望能够在特定条件下动态呈现我的 index.html 中的 div/脚本。我想做的抽象如下:
class MainHandler(webapp2.RequestHandler):
def get(self, q):
script = "<script>function display_alert() alert('Hello world!'</script> <div>hello world</div>"
if q is None:
q = 'index.html'
path = os.path.join (os.path.dirname (__file__), q)
self.response.headers ['Content-Type'] = 'text/html'
self.response.write (template.render (path,
"script" : script
))
def main ():
application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
util.run_wsgi_app (application)
if __name__ == '__main__':
main ()
HTML 是一个简单的 index.html 文件,其中包含 script。当它渲染出来时,它看起来像这样:
为什么我给出的 HTML 渲染不正确?我尝试通过 simplejson.dumps 运行“脚本”变量,它也不起作用。
【问题讨论】:
【参考方案1】:我相信 django 模板中默认会转义 Html
在 django 中,这是通过 safe
模板过滤器完成的
script|safe
您是否使用 django 来呈现您的模板?还是不同的引擎?
【讨论】:
谢谢,这行得通!我最终通过在 django 模板中使用 for 循环解决了我的问题:docs.djangoproject.com/en/dev/ref/templates/builtins以上是关于使用 Python/GAE 动态生成 HTML div/JS 脚本的主要内容,如果未能解决你的问题,请参考以下文章
使用 Python/(GAE) 为 Android 应用推送通知的服务器端实现 [关闭]
是否可以将 GCM 与 python GAE 后端一起使用?