PROTOCOL
PROTOCOL

Reputation: 371

Flask - SQLAlchemy : Code to check the username and password

I am doing a Flask-login system(by not using inbuilt LoginManager). I have a query to check the username and password which is :

 query = s.query(Users).filter(Users.email.in_([POST_email]), Users.password.in_([POST_PASSWORD]) )
 result = query.first()

This query is not working. It returns None in the console. Please give a query to check whether the data given in the forms match with the database values.

**

  1. Here Users is the Data Model.
  2. email and password are two columns
  3. POST_email and POST_password are two values that are received from forms
  4. s is the instance of Session s = Session()

**

This is the function code.

def do_admin_login():
   POST_USERNAME = str(request.form['username'])
   POST_PASSWORD = str(request.form['password'])

   Session = sessionmaker(bind=engine)
   s = Session()
   query = s.query(Users).filter(Users.email.in_([POST_USERNAME]), Users.password.in_([POST_PASSWORD]) )
   result = query.first()
   if result:
       session['logged_in'] = True
   else:
       flash('wrong password!')
return home()

The

POST_USERNAME 

and

 POST_PASSWORD

contain the values that are received from the forms in HTML page. I use Postgres database.

Upvotes: 0

Views: 7781

Answers (1)

Dipnesh Parakhiya
Dipnesh Parakhiya

Reputation: 542

Try this code

query = s.query(Users).filter(Users.email==POST_USERNAME, Users.password==POST_PASSWORD)

Upvotes: 1

Related Questions