使用Google App Engine我希望通过这个问题学习一些非常基础的知识。我希望能够在打开应用程序时打开放置在文件夹中的index.html页面。
我使用' Google App Engine Launcher'
生成了一个新应用程序我稍微修改了app.yaml,它现在看起来像以下......
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /templates
static_dir: templates
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
我还添加了一个名为' templates'。
的目录在目录中我放置了一个名为' index.html'的文件。
<html>
<header><title>This is title</title></header>
<body>
Hello world cls
</body>
</html>
我的main.py尚未修改,因此看起来像
#!/usr/bin/env python
#
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world!')
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
关于我需要做什么或改变以找到成功的任何想法?
此致
克里斯
&GT; 由于Gwyn的评论
,我的代码发生了很大的变化我现在从链接(https://console.developers.google.com/start/appengine)获得标准的Django模板代码 Gwyn最终index.html文件将不仅仅是静态页面,因此您描述的直接URL无法正常工作。你确实教过我,随着我的进步,这会派上用场。一旦我得到了基础知识,我想引入一些Polymer代码......
所以,如果有人可以帮助我从一个模板中获取一个你好的世界&#39;使用来自Google Developers Console生成的标准django代码集的index.html页面的文件夹,那么您的答案将非常受欢迎。
此致
克里斯
答案 0 :(得分:0)
首先,要使用HTML,你需要一个模板引擎,在app引擎中你可以使用这个EZT, Cheetah, ClearSilver, Quixote, Django, and Jinja2但为了简单起见,你可以修改你的代码直接发送html
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('<html><header><title>This is title</title></header><body>Hello world cls</body></html>')
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
但正如Gwyn Howell所说,对于更复杂的东西,你必须使用模板引擎
答案 1 :(得分:0)
我想更新这个,所以如果其他人都在努力,他们也可能会找到成功...... 我要感谢Gwyn Howell和Kristian Damian。我使用你的两个评论为其他有同样问题的人提出了答案。
application: helloworld version: 2 runtime: python27 api_version: 1 threadsafe: yes handlers: - url: /favicon\.ico static_files: favicon.ico upload: favicon\.ico - url: .* script: main.app libraries: - name: webapp2 version: latest - name: jinja2 version: latest
import os import webapp2 import jinja2 env = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__),
'模板')))
class MainHandler(webapp2.RequestHandler): def get(self): self.response.write('Hello world! <a href="/about_v2.html">About</a>.<br />') class AboutPage_v2(webapp2.RequestHandler): def get(self): template_values = { } template = env.get_template('about_v2.html') self.response.out.write(template.render(template_values)) app = webapp2.WSGIApplication([ ('/', MainHandler), ('/about_v2.html', AboutPage_v2)], debug=True)
我希望这个答案有助于其他人。