0

Alright! So, I was creating a simple Login Program for a recent website that I was working on! I'm using Flask to serve my HTML Page Templates.... I'm using the sqlite3 Python module for easy usage of modules...

So, I created an App Route for my main website for the login pages.. You can check the code below:

def login():
    msg = ''
    
    if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
        # Create variables for easy access
        username = request.form['username']
        password = request.form['password']

        #Connection Est.
        connection = sqlite3.connect("Users.db")

        #cursor
        USer_crsr = connection.cursor()

        user_cm = """SELECT username, password FROM accounts"""

        USer_crsr.execute(user_cm)
        USer_result = USer_crsr.fetchall()

        if username and password in USer_result:
            # Create session data, we can access this data in other routes
              session['loggedin'] = True
              session['id'] = USer_result['id']
              session['username'] = USer_result['username']
              # Redirect to home page
              return 'Logged in successfully!'
        else:
            # Account doesnt exist or username/password incorrect
            msg = 'Incorrect username/password!'
    return render_template("login.html", msg=msg)

I run my program and I get no errors at all! I open my login page and the page opens without raising an error! Here's the page look: Login Page

But when I Submit my form, I get an Error (not in the console, but in the webpage itself) : 405 - Method Not allowed!

Also, here's the code for the database file I used to create my database :


#Connection to FILE
User_DATA_connection = sqlite3.connect('Users.db')

crsr = User_DATA_connection.cursor()

#command here
command = """CREATE TABLE IF NOT EXISTS 'accounts' (
  'id' int(11) NOT NULL,
  'username' varchar(50) NOT NULL,
  'password' varchar(250) NOT NULL,
  'email' varchar(100) NOT NULL,
  PRIMARY KEY ('id')
) 
"""

#execute
crsr.execute(command)

comm = """INSERT INTO accounts VALUES (1, 'test', 'test', 'test@gmail.com');"""

crsr.execute(comm)

User_DATA_connection.commit()

#close data
User_DATA_connection.close()

1 Answers1

0

When you decorate your Flask route's function, you need to allow the POST method. Example:

@app.route('/login', methods=["GET", "POST"])
def login():
    if request.method == "GET":
        # Render your page
    if request.method == "POST":
        # Process the data you POST-ed from your frontend (insert them into the DB, etc.)
neisor
  • 384
  • 4
  • 15
  • Ok, I'll try doing that... – Alston V. Abraham Dec 22 '21 at 09:43
  • It worked, but it displays the message 'Incorrect username/password!', when I have already inserted the values 'test' into the 'username' and 'password' fields.....Is there any other problem? – Alston V. Abraham Dec 22 '21 at 09:47
  • Seems like you're wrongly comparing the values from the database against the values you used in the login form. I would try to look at `if username and password in USer_result:` in your code. – neisor Dec 22 '21 at 09:52
  • I didn't understand what you're trying to say here.... – Alston V. Abraham Dec 22 '21 at 09:57
  • If you are really using the correct credentials to log in (test, test), then it seems like you are wrongly comparing the values that you have written in your login form against the data from the database. – neisor Dec 22 '21 at 10:02
  • I don't know, I went through my code again but there seems to no error.... should I try using some other method to compare the values of the user input put with the values in the database? If yes, then what method? – Alston V. Abraham Dec 22 '21 at 10:04
  • For getting proper help on your next issue, please, open a new question so that a new answer can be posted to help you out for your next issue. – neisor Dec 22 '21 at 10:14
  • Ok, I'll open a new issue! – Alston V. Abraham Dec 22 '21 at 10:18
  • @AlstonV.Abraham if my answer helped to solve the issue that you have written about here, please, accept my answer. In this question you have asked regarding the `Method not allowed` error, which was fixed by my example. – neisor Dec 22 '21 at 11:41