我已经在Heroku上成功部署了Flask / PostgreSQL Web应用程序。它应该由两页组成。第二页应打开。但是,我得到的是标题中描述的错误。
这是我的第一个Web应用程序。我在哪里以及如何查找错误?感谢您的任何帮助。
```from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from send_email import send_email
from sqlalchemy.sql import func
app=Flask(__name__)
# app.config['SQLALCHEMY_DATABASE_URI']='postgresql://postgres:***@localhost/height_collector'
app.config['SQLALCHEMY_DATABASE_URI']='postgres://noiilxmwxinrrz:3ebb4e8e5f6002a4340466bcfba5272dd491dbafbc04aa3523154c1ffc10936b@ec2-52-86-73-86.compute-1.amazonaws.com:5432/db1dr24ra03kco?sslmode=require'
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"]
if db.session.query(Data).filter(Data.email_==email).count() == 0:
data=Data(email,height)
db.init_app(app)
db.session.add(data)
db.session.commit()
average_height=db.session.query(func.avg(Data.height_)).scalar()
average_height=round(average_height,1)
count=db.session.query(Data.height_).count()
send_email(email,height,average_height,count)
return render_template("success.html")
return render_template('index.html',
text="Seems like we've got something from that email address already!")
if __name__ == '__main__':
app.debug=True
app.run()
from email.mime.text import MIMEText
import smtplib
def send_email(email, height,average_height,count):
from_email="***.com"
from_password="***"
to_email=email
subject="Height data"
message="Hey there, your height is <strong>%s</strong> cm. <br> Average height of all is <strong>%s</strong> cm, and that is calculated out of <strong>%s</strong> of people. <br> Thanks! " % (height, average_height, count)
msg=MIMEText(message, 'html')
msg['Subject']=subject
msg['To']=to_email
msg['From']=from_email
gmail=smtplib.SMTP('smtp.gmail.com',587)
gmail.ehlo()
gmail.starttls()
gmail.login(from_email, from_password)
gmail.send_message(msg) ```
答案 0 :(得分:0)
一个问题是成功路线上未启用GET方法。
因此将其更改为:
@app.route("/success", methods=['GET','POST'])
另一个问题是您尚未安装psycopg2
模块。您需要在requirements.txt
文件中进行此操作。
if db.session.query(Data).filter(Data.email_==email).count() == 0:
语句的评估结果为False时,您也不会呈现模板。
可能还有其他问题。