我能够成功地部署应用程序而没有任何错误,但是当我访问应用程序 URL 时,我收到错误消息:
Error: Not Found The requested URL / was not found on this server.
当我访问云控制台上的错误日志时,我看到错误:
ValueError: virtualenv: cannot access flask: No such virtualenv or site >directory
我的代码如下:
应用程序.py
from flask import Flask, render_template, request,url_for,redirect,flash
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
app = Flask(__name__)
app.secret_key = 'some_secret'
engine = create_engine("postgresql://postgres:sona@localhost:1234/users")
db = scoped_session(sessionmaker(bind=engine))
@app.route("/")
def index():
return render_template("index.html")
@app.route("/register")
def register():
return render_template("register.html")
@app.route("/hello", methods=["GET","POST"])
def hello():
if request.method=="GET":
return redirect(url_for('index'))
else:
fname = request.form.get("fname")
fname = fname.capitalize()
lname = request.form.get("lname")
lname = lname.capitalize()
username = request.form.get("username")
password = request.form.get("password")
email = request.form.get("email")
mobile = request.form.get("mobile")
age = request.form.get("age")
location = request.form.get("location")
if fname == '' and age == '':
flash('Invalid Credentials !!')
return render_template("register.html",message='True')
else:
db.execute("INSERT INTO users(fname, lname, username, password,
email, mobile, age, location) VALUES (:fname, :lname,
:username, :password,
:email, :mobile, :age, :location)",
{"fname": fname, "lname": lname,"username": username,
"password": password, "email": email, "mobile": mobile,
"age": age, "location": location})
db.commit()
users = db.execute("SELECT * FROM users").fetchall()
return render_template("hello.html",fname=fname,lname=lname,
username=username,password=password,
email=email,mobile=mobile,age=age,
location=location,users=users)
if __name__ == '__main__':
app.debug = True
app.run()
appengine_config.py
from google.appengine.ext import vendor
# Add any libraries installed in the "lib" folder.
vendor.add('flask')
应用.yaml
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /static
static_dir: static
- url: /.*
script: main.app
需求.txt
Flask==0.12.2
Werkzeug<0.13.0,>=0.12.0
其余的所有 HTML 文件都很好,我能够在本地主机服务器上成功运行该应用程序。 还需要指导将应用程序连接到云上的 postgresql 实例。 此外,在 cloud sdk shell 上的 ocalhost 上测试应用程序时,应用程序也没有给出任何错误。
请您参考如下方法:
将 application.py 重命名为 main.py(或者,非标准,更改 app.yaml 文件 main .app 和 application.app,但不是两者)。包含 app 变量的模块名称必须匹配。来自 Handlers element table from the app.yaml reference 中的 script 行:
A
script:directive must be a python import path, for example,package.module.appthat points to a WSGI application. The last component of ascript:directive using a Python module path is the name of a global variable in the module: that variable must be a WSGI app, and is usually calledappby convention.
您的 vendor.add('flask') 建议您为第 3 方库使用非标准目录名称 - flask 而不是 lib - 请务必相应地调整文档信息。但我建议恢复到标准命名约定,除非你故意找麻烦。
删除 if __name__ == '__main__': 部分 - 这不是 Flask 在 GAE 上的工作方式。
我强烈建议仔细阅读 Getting Started with Flask on App Engine Standard Environment (包括代码示例)。
