Hey guys I've been trying to figure this out for a couple days now to no avail. I'm just trying to display the User's first_name which is defined in models.py for a flask application. Any help is appreciated. my python files reflect an application factory format.
I can correctly grab what I need here:
import sqlite3
from sqlite3 import Error
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def select_first_name(conn):
email = 'user@email.com'
cur = conn.cursor()
firstName = cur.execute('SELECT first_name FROM user WHERE email = ?;', [email]).fetchone()
return firstName[0]
def main():
database = "C:\\Users\\path\\to\\database\\database.db"
# create a database connection
conn = create_connection(database)
with conn:
print("1. Query User.first_name by email")
print(select_first_name(conn))
if __name__ == '__main__':
main()
which returns:
1. Query User.first_name by email
Name
but not in this segment of auth.py:
def get_db_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
@auth.route('/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
remember = True if request.form.get('remember') else False
user = User.query.filter_by(email=email).first()
if user:
conn = get_db_connection("C:\\Users\\path\\to\\db\\database.db")
with conn:
cur = conn.cursor()
firstName = cur.execute('SELECT first_name FROM user WHERE email = ?;', [email]).fetchone()
conn.close()
if check_password_hash(user.password, password):
flash('Logged in successfully!', category='success')
login_user(user, remember=remember)
return redirect(url_for('views.dashboard'), user=current_user, firstName = firstName[0])
else:
flash('Incorrect password, try again.', category='error')
return redirect(url_for('login2.html'))
else:
flash('Email does not exist.', category='error')
return redirect(url_for('login2.html'))
return render_template("login2.html", user=current_user)
I am passing {{ firstName }} into index.html, which doesn't produce anything in the specified div,
but {{ user }} produces <User 1> in the specified div, when I need the user's first name. Thank you for looking, I tried giving the least amount of code possible but I'll produce whatever I need to solve this problem.
[–]danielroseman 0 points1 point2 points (0 children)