【发布时间】:2017-12-26 22:54:41
【问题描述】:
我正在使用烧瓶记录高度的 Udemy 课程。我现在正在使用 PostgreSQL,我已经安装了它,并且我已经完全复制了他的代码:
from flask import Flask, render_template, request
from flask.ext.sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config(['SQLALCHEMY_DATABASE_URI']='postgresql://postgres:password
@localhost/height_collector')
db=SQLAlchemy(app)
class Data(db.Model):
__tablename__='data'
id=db.Column(db.Integer, primary_key=True)
email_=db.Column(db.String(120), unique=True)
height_=db.Column(db.Integer)
def __init__(self, email_, height_):
self.email_=email_
self.height_=height_
@app.route("/")
def index():
return render_template("index.html")
@app.route("/success", methods=["post"])
def success():
if request.method=='POST':
email=request.form['email_name']
height=request.form['height_name']
print(height,email)
return render_template("success.html")
if __name__=='__main__':
app.debug=True
app.run()
问题来了,当他说要在虚拟环境中运行 python,然后输入 :db.create_all() 在 PostgreSQL 中创建数据库时,我得到了这个错误: 文件 ,第 1 行 NameError: 名称 'db' 未定义
不知道如何继续,任何意见将不胜感激。
【问题讨论】:
标签: python html postgresql python-3.x flask