[英]Need help on getting started with google app engine(PYTHON 2.7)
[英]Getting Started with Python in Google App Engine
我剛開始使用google app引擎使用webapp2框架和jinja2模板進行python。 我似乎無法啟動並運行我的第一個非常簡單的腳本。 我想要的只是腳本提供index.html文件(位於同一目錄中)。
這是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
這是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)
更新:我在Google應用引擎啟動器中本地運行此功能。 當我嘗試打開文件時,我收到服務器錯誤的描述
The website encountered an error while retrieving http://localhost:9080/. It may be
down for maintenance or configured incorrectly."
這就是您的代碼無法運行的原因:
以下是我認為您的代碼應該是這樣的:
的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
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)
我建議你做以下事情,讓你的生活更輕松:
希望這有助於您前進。
快樂編碼:)
在webapp2中,您應該使用 app而不是application ,所以最后一行應如下所示:
app = webapp2.WSGIApplication([('/', MainPage),], debug=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.