使用Google App Engine如何打开html页面?

时间:2014-08-27 15:16:11

标签: google-app-engine python-2.7

使用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页面的文件夹,那么您的答案将非常受欢迎。

此致

克里斯

2 个答案:

答案 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。我使用你的两个评论为其他有同样问题的人提出了答案。

  1. 我澄清说我正在使用下载的适用于Google App Engine的Python SDK
  2. 我使用File | Create New Application - Python 2.7
  3. 制作了一个示例python项目
  4. 我运行该项目以确保它在没有任何更改的情况下正常工作
  5. 然后我决定使用'Jinja2'作为我的模板引擎
  6. 我制作了一个'templates'文件夹并将'about_v2.html'文件放在其中
  7. 我将'app.yaml'代码修改为
  8. 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
    
    1. 我将'main.py'代码修改为
    2. 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)
      
      1. 然后,我在本地计算机上运行该项目,并且能够访问网址
      2. 我希望这个答案有助于其他人。