title: Getting logged out of session when clicking links in nav bar for specific html pages in Python-Flask app
body: <p>I have a question about redirects in my Python-Flask app found here: <a href="http://jordanirvine.pythonanywhere.com/" rel="nofollow noreferrer">http://jordanirvine.pythonanywhere.com/</a> 
  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:</p>
  
  <p>Here is is the Home link:</p>
  
  <pre><code>@app.route('/home/&lt;defSess&gt;')
  @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)
  </code></pre>
  
  <p>Here is the MemberSearch link:</p>
  
  <pre><code>#memberSearch (Dashboard)
  @app.route('/memberSearch/', defaults={'defSess': None})
  @app.route('/memberSearch/&lt;defSess&gt;')
  @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)
  </code></pre>
  
  <p>And here is the add_member code:</p>
  
  <pre><code># Add member
  @app.route('/add_member/&lt;defSess&gt;', 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)
  </code></pre>
  
  <p>Some other functions and code:</p>
  
  <p>Logout code:</p>
  
  <pre><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'))
  </code></pre>
  
  <p>Check to see if user is logged in:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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! </p>