2

I have a question about redirects in my Python-Flask app found here: http://jordanirvine.pythonanywhere.com/ I have been making steady progress with this app as a new developer, but recently I am having trouble with certain aspects. If you log into the app using 'username' and 'password' for the username and password, and then click the 'add member' button, you will see that everything works fine, and you are able to add a member easily, however, if you are on the 'add member' web page and decide to go back to the 'Search Members' or 'Home" page at the top without pressing submit it logs you out of the current session. Can someone help me understand why it is doing this? I would like the user to stay logged in during these transitions as that will probably happen often. Below is some source code:

Here is is the Home link:

@app.route('/home/<defSess>')
@app.route('/home/', defaults={'defSess': None})
def home(defSess):

if defSess is None:
    session.clear()
    return render_template('home.html')
else:
    defSess = defSess

    with sql.connect(databases) as connection:

        connection.execute("CREATE TABLE IF NOT EXISTS dataBaseNames(name varchar(30) DEFAULT NULL)")

        return render_template('home.html', defSess=defSess)

Here is the MemberSearch link:

#memberSearch (Dashboard)
@app.route('/memberSearch/', defaults={'defSess': None})
@app.route('/memberSearch/<defSess>')
@is_logged_in
def memberSearch(defSess):

if defSess is None:
    session.clear()
    flash('Unauthorized, Please login', 'danger')
    return render_template('login.html')
else:
    defSess = defSess

createTables(defSess)

with sql.connect(defSess) as connection:
    connection.row_factory = sql.Row
    cur = connection.execute("SELECT * from members m join visits v 
where v.breakfastDate = (select max(visits.breakfastDate) from visits 
where visits.clientId = m.clientId) UNION select * from members m left 
join visits v on m.clientId = v.clientId where breakfastDate is NULL")

    members = build_dict_list(cur)

    if members:
        return render_template('memberSearch.html', members=members, defSess=defSess)
    else:
        msg = 'No members Found'
        return render_template('memberSearch.html', msg=msg, defSess=defSess)

And here is the add_member code:

# Add member
@app.route('/add_member/<defSess>', methods=['GET', 'POST'])
def add_member(defSess):
form = MemberForm(request.form)
if request.method == 'POST' and form.validate():

    with sql.connect(defSess) as connection:

        name = form.name.data
        author = defSess
        connection.execute("INSERT INTO members(name, author) 
VALUES(?,?)",[name,author],)

        flash('Member Created', 'success')

        return redirect(url_for('memberSearch', defSess=defSess))

return render_template('add_member.html', form=form)

Some other functions and code:

Logout code:

@app.route('/logout/')
@is_logged_in
def logout():
session.clear
flash('You are now logged out', 'success')
session['logged_in'] = False
return redirect(url_for('login'))

Check to see if user is logged in:

def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
    if 'logged_in' in session:
        return f(*args, **kwargs)
    else:
        flash('Unauthorized, Please login', 'danger')
        return redirect(url_for('login'))
return wrap

If you need more code including the HTML code please let me know, otherwise any help would be much appreciated! Also, I realize there was probably a more efficient way to do write this app, but i'm inexperienced and this is my first attempt at an app. Anyway, thanks in advance!

0 Answers0