please, don't post images of code. Help us to help you -
https://idownvotedbecau.se/imageofcode
Copy/paste the code - in python tags, any traceback - in full, in error tags. if there is a lot of code - consider providing minimal reproducible example.
See
BBcode help for more info.
sorry about that i changed it.
looking at your python code
import os
import psycopg2
from flask import Flask, session, render_template
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
app = Flask(__name__)
# Check for environment variable
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
@app.route("/")
def index():
return "Project 1: TODO"
try:
connection = psycopg2.connect(user = "***",
password = "***",
host = "***",
port = "***",
database = "***")
cursor = connection.cursor()
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
@app.route("/register", methods=["GET", "POST"])
def register():
return render_template("register.html")
password = request.form.get("password")
username = request.form.get("username")
session.clear()
db.execute("INSERT INTO users (username, password) VALUES (:username, :password")
db.commit()
On line 58 you always return, so the rest of the code is never executed.
By the way, remove your personal data - username, apssword, etc. from pastebin and anyway change them for security reasons
i did change them. thank you. I added that line recently - it doesn't work without it either. Same thing.
you need to check what request.method is
if method is GET - render the register.html
if method is POST - process form data and render page that confirm registration (or again register.html if you wish)
actually, when i delete that return line, i get an error saying NameError: name 'request' is not defined:
OK i imported "request".
Then I moved the return register.html line to after dbcommit(). now i'm getting an error that says:
sqlalchemy.exc.StatementError
sqlalchemy.exc.StatementError: (sqlalchemy.exc.InvalidRequestError) A value is required for bind parameter 'username'
[SQL: INSERT INTO users (username, password) VALUES (%(username)s, %(password)s]
[parameters: [{}]]
(Background on this error at:
http://sqlalche.me/e/cd3x)
you need to import it
from flask import request
Also, you can use extension like Falsk-WTF to make it easier to validate and process forms