title: Why is my session['username'] is always the last entry from my database instead of current one in flask?
body: <p>I got the simplest db:</p>
  
  <pre><code>class users(db.Model):
      _id = db.Column("id", db.Integer, primary_key=True)
      username = db.Column('username', db.String(100))
      password = db.Column('password', db.String(100))
  </code></pre>
  
  <p>here are my login, register, user and logout functions:</p>
  
  <pre><code>@app.route('/login', methods=['POST', 'GET'])
  def login():
      error = None
      if request.method == "POST":
          username = request.form['username']
          password = request.form['password']
          found_user = users.query.filter_by(username=username).first()
          found_pass = users.query.filter_by(password=password).first()
          if found_user and found_pass:
              session['logged_in'] = True
              flash("You've just logged in!")
              return redirect(url_for('user'))
          else:
              error = 'Invalid Data. Please try again.'
      return render_template('login.html', error=error)
  
  
  @app.route('/register', methods=['POST', 'GET'])
  def register():
      if request.method == "POST":
          username = request.form['username']
          password = request.form['password']
          session['username'] = username
          session['password'] = password
          found_user = users.query.filter_by(username=username).first()
          if found_user:
              flash("User already registered!")
          elif not request.form['username'] or not request.form['password']:
              flash("Enter valid data!")
          else:
              usr = users(username, password)
              db.session.add(usr)
              db.session.commit()
              flash("User registered!")
      return render_template('register.html')
  
  
  @app.route('/user')
  @login_required
  def user():
      if session.get('logged_in'):
          return render_template('user.html')
      else:
          return redirect(url_for('login'))
  
  
  @app.route('/logout')
  @login_required
  def logout():
      session.pop('logged_in', None)
      flash("You've just logged out!")
      return redirect(url_for('login'))
  </code></pre>
  
  <p>I need to get the current session['username'] to display it on an html template, but each time i call it and pass the argument to html through user function, like this:</p>
  
  <pre><code>@app.route('/user')
  @login_required
  def user():
      user = session['username']
      if session.get('logged_in'):
          return render_template('user.html', user=user)
      else:
          return redirect(url_for('login'))
  </code></pre>
  
  <p>it returns the last database entry of a username I've registered instead of the one that is currently logged in, can someone explain why this is happening?</p>