Python Forum
Need help with code - thanks!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with code - thanks!
#1
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
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
sorry about that i changed it.
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
i did change them. thank you. I added that line recently - it doesn't work without it either. Same thing.
Reply
#6
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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)
Reply
#8
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Fix your sql statement
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
I don't see the error
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020