0

I have successfully deployed my Flask/PostgreSQL web app on Heroku. It's supposed to consist of two pages. The second page should open. However, instead I get the error described in the title.

This is my first web app. Where and how do I look for the error? Any help is appreciated, thank you for your patience.

    ```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) ```

Image of Heroku log output

ayy24
  • 23
  • 6
  • "where do I look for my mistake?"—start by checking your logs. Since you're using Heroku, start by running `heroku logs`. ([HTTP 500](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_Error) is a generic server-side error message. On its own it doesn't tell us anything useful. Any time you see this your first step should be to check your error logs for more detail.) – ChrisGPT was on strike Apr 19 '20 at 19:17

1 Answers1

0

One issue is that the GET method is not enabled on the success route.
So change it to:

@app.route("/success", methods=['GET','POST'])

Another issue is that you haven't installed the psycopg2 module. You would need to do that in a requirements.txt file.

Also you're not rendering a template when the if db.session.query(Data).filter(Data.email_==email).count() == 0: statement evaluates to False.

There may be other issues.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223