簡體   English   中英

Google App Engine中的Python入門

[英]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格式不正確
  • 環境錯誤
  • 你錯過了第5行的結束括號
  • 您尚未導入jinja2庫
  • 變量__ FILE __未聲明

以下是我認為您的代碼應該是這樣的:

的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM