Need help with code - thanks! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Need help with code - thanks! (/thread-27432.html) Pages:
1
2
|
Need help with code - thanks! - card51shor - Jun-06-2020 Hey guys I have tried for days and can't figure out why this isn't adding anything to my users table. I'm not getting any errors - it just doesn't do anything when I press submit. Here is all the necessary code. Thanks for looking! HTML: https://pastebin.com/ExyGmJJQ Python: https://pastebin.com/QjHX3zqG Terminal: https://www.picpasteplus.com/v.php?i=36561f0e2d Table: https://www.picpasteplus.com/v.php?i=78ebf052a2 RE: Need help with code - thanks! - buran - Jun-06-2020 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. RE: Need help with code - thanks! - card51shor - Jun-06-2020 sorry about that i changed it. RE: Need help with code - thanks! - buran - Jun-06-2020 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 RE: Need help with code - thanks! - card51shor - Jun-06-2020 i did change them. thank you. I added that line recently - it doesn't work without it either. Same thing. RE: Need help with code - thanks! - buran - Jun-06-2020 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) RE: Need help with code - thanks! - card51shor - Jun-06-2020 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) RE: Need help with code - thanks! - buran - Jun-06-2020 you need to import it from flask import requestAlso, you can use extension like Falsk-WTF to make it easier to validate and process forms RE: Need help with code - thanks! - buran - Jun-06-2020 Fix your sql statement RE: Need help with code - thanks! - card51shor - Jun-06-2020 I don't see the error |