I'm going through the testdriven.io course "Developing Web Applications with Python and Flask"
The question is in the Part 6: Deploying to Render I am using the free tier service in Render platform. So do not have access to the Shell console. So database initialization was done using Python code as suggested in the course.
def create_app():
app = Flask(__name__)
config_type = os.getenv('CONFIG_TYPE', default = 'config.DevelopmentConfig')
app.config.from_object(config_type)
initialize_extensions(app)
register_blueprints(app)
configure_logging(app)
register_app_callbacks(app)
register_error_pages(app)
##############################################################################
# This section is only necessary in production when a command-line interface #
# is NOT available for running commands to initialize the database. #
##############################################################################
import sqlalchemy as sa
# Check if the database needs to be initialized
engine = sa.create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
inspector = sa.inspect(engine)
if not inspector.has_table("users"):
with app.app_context():
database.drop_all()
database.create_all()
app.logger.info('Initialized the database!')
else:
app.logger.info('Database already contains the users table.')
return app
I got into errors when I try to register a new user. The new user registration post request after clicking submit button is returning the html page: "Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application."
The below is the Postgresql logs from Render platform:
[664f1553.af6-4] user=navin_flask_stock_portfolio_database_user,db=navin_flask_stock_portfolio_database,app=[unknown],client=10.210.94.32,ERROR: value too long for type character varying(128) [664f1553.af6-5] user=navin_flask_stock_portfolio_database_user,db=navin_flask_stock_portfolio_database,app=[unknown],client=10.210.94.32,STATEMENT: INSERT INTO users (email, password_hashed, registered_on, email_confirmed, email_confirmation_sent_on, email_confirmed_on) VALUES ('[email protected]', 'scrypt:32768:8:1$U1b1ercBqyk3HjSb$2b3a70b9f1f848cec479cd03538b9c0d844831866ad9f09d93a2bbd0680493e3b10c3cf7d0ad5506efe3442954957455b0d6e6c21472ca480931e81fb472dccd', '2024-05-23T10:07:15.446760'::timestamp, false, '2024-05-23T10:07:15.446782'::timestamp, NULL) RETURNING users.id [664f1553.af6-6] user=navin_flask_stock_portfolio_database_user,db=navin_flask_stock_portfolio_database,app=[unknown],client=10.210.94.32,LOG: disconnection: session time: 0:16:02.193 user=navin_flask_stock_portfolio_database_user database=navin_flask_stock_portfolio_database host=10.210.94.32 port=41878 [664f14b6.a5c-4] user=navin_flask_stock_portfolio_database_user,db=navin_flask_stock_portfolio_database,app=[unknown],client=10.210.94.32,LOG: disconnection: session time: 0:18:40.703 user=navin_flask_stock_portfolio_database_user database=navin_flask_stock_portfolio_database host=10.210.94.32 port=33938
The below is the Flask app logs from Render platform:
127.0.0.1 - - [23/May/2024:10:06:15 +0000] "GET /users/register HTTP/1.1" 200 1921 "https://navin-flask-stock-portfolio.onrender.com/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" 127.0.0.1 - - [23/May/2024:10:06:15 +0000] "GET /static/css/form_style.css HTTP/1.1" 200 0 "https://navin-flask-stock-portfolio.onrender.com/users/register" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" 127.0.0.1 - - [23/May/2024:10:06:15 +0000] "GET /static/css/base_style.css HTTP/1.1" 304 0 "https://navin-flask-stock-portfolio.onrender.com/users/register" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" 127.0.0.1 - - [23/May/2024:10:07:15 +0000] "POST /users/register HTTP/1.1" 500 265 "https://navin-flask-stock-portfolio.onrender.com/users/register" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" 127.0.0.1 - - [23/May/2024:10:07:15 +0000] "GET /favicon.ico HTTP/1.1" 404 1350 "https://navin-flask-stock-portfolio.onrender.com/users/register" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
I don't know why the Flask App in running fine on my local development environment but fails on Render platform.. I see 500 status code error on the logs. May I know what I am doing wrong ..
tried the same couple of times on render.. the above error is consistent