简体   繁体   English

Google App Engine中的Python入门

[英]Getting Started with Python in Google App Engine

I'm just getting started in python with google app engine using the webapp2 framework and jinja2 templating. 我刚开始使用google app引擎使用webapp2框架和jinja2模板进行python。 I can't seem to get my first and very simple script up and running. 我似乎无法启动并运行我的第一个非常简单的脚本。 All I want is for the script to serve the index.html file(located in the same directory). 我想要的只是脚本提供index.html文件(位于同一目录中)。

Here is the app.yaml file: 这是app.yaml文件:

libraries
- name: webapp2
  version: latest
- name: jinja2
  version: latest

application: practice
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: practice.application

Here is practice.py: 这是practice.py:

import os
import webapp2
from jinja2 import Enviroment, FileSystemLoader

loader = jinja2.FileSystemLoader(os.path.dirname(__FILE__)
env = jinja2.Enviroment(loader)

class MainPage(webapp2.RequestHandler):
    def get(self):
        template = env.get_template('index.html')
        self.response.write(template.render())

application = webapp2.WSGIApplication([
 ('/', MainPage),
 ], debug=True)

Update: I am running this locally from the Google app engine launcher. 更新:我在Google应用引擎启动器中本地运行此功能。 When I attempt to open the file I receive a server error with the description 当我尝试打开文件时,我收到服务器错误的描述

The website encountered an error while retrieving http://localhost:9080/. It may be
down for maintenance or configured incorrectly."

Here's why your code won't run: 这就是您的代码无法运行的原因:

  • Your app.yaml is malformed 你的app.yaml格式不正确
  • Enviroment is spelt wrong 环境错误
  • Your missing a closing bracket on line 5 你错过了第5行的结束括号
  • You haven't imported the jinja2 library 您尚未导入jinja2库
  • The variable __ FILE __ is undeclared 变量__ FILE __未声明

Here's what I think your code should look like: 以下是我认为您的代码应该是这样的:

app.yaml 的app.yaml

application: practice
version: 1
runtime: python27
api_version: 1
threadsafe: true

libraries:
- name: webapp2
  version: latest
- name: jinja2
  version: latest

handlers:
- url: /.*
  script: practice.application

practice.py practice.py

import jinja2
import os
import webapp2

loader = jinja2.FileSystemLoader(os.path.dirname(__file__))
env = jinja2.Environment(loader=loader)

class MainPage(webapp2.RequestHandler):
    def get(self):
        template = env.get_template('index.html')
        self.response.write(template.render())

application = webapp2.WSGIApplication([
 ('/', MainPage),
 ], debug=True)

I suggest you do the following to make your life a LOT easier: 我建议你做以下事情,让你的生活更轻松:

Hope this helps get you on your way. 希望这有助于您前进。

Happy coding :) 快乐编码:)

webapp2中,您应该使用 app而不是application ,所以最后一行应如下所示:

app = webapp2.WSGIApplication([('/', MainPage),], debug=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM