Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:28:45+00:00 2026-06-15T05:28:45+00:00

When a user logs into my sample application I add a username key into

  • 0

When a user logs into my sample application I add a username key into the session object and I hang a user object off of the global “g” object. See below.

@app.route('/login/', methods=['GET', 'POST'])
def login():
    """Logs the user in."""

    error = None
    if request.method == 'POST':
        print("login hit")
        user = get_user(request.form['username'])
        if user is None:
            error = 'Invalid username'
            print error
        elif not check_password_hash(user.password, request.form['password']):
            error = 'Invalid password'
            print error
        else:
            flash('You were logged in')
            print "logged in"
            session['username'] = request.form['username']
            g.user = request.form['username']
            print error, "error"
            return redirect(url_for('main'))

Now, if the user logs in successfully (which is what is happening), he will redirect to the main page. This will teardown the old request and spawn a new one. The problem is that when a new request starts the session is wiped. I am printing out the session keys and they are all empty. This only happens on heroku, not on my local machine. What am I doing wrong.

  @app.before_request
    def before_request():
        print session.keys(), session.values()  # shows [] []
        print("before request")
        print ('username' in session, "in session?")
        g.db = connect_db()
        g.user = None
        if "username" in session:
            g.user = get_user(session['username'])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:28:46+00:00Added an answer on June 15, 2026 at 5:28 am

    Well, seeing as this indeed would be the answer due to multiple workers I’ll move it from the comments part. 🙂 You don’t want redis at the gunicorn layer, rather in your application. These four links should help you out:

    1) First, enable/install Redis in Heroku

    2) Then check this out (will replace Flasks entire session driver with one based on redis):
    This might be overkill, I’d simply use redis as a key-storage value in the app (check my third link) but you might like this approach.

    3) Not as related but shows some usage examples (queue system for flask with redis)

    4) Basic redis tutorial for python

    These three links should get you started, if it’s not enough I’ll dig up some more examples so just ask if you need them!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When a user logs into my site, I need to persist that login session.
I'm working on an e-commerce application. When a user logs into my app, I
I have a pop-up window a user logs into, once they are logged in
I have a weird quirk with cookies in IE. When a user logs into
I have been asked to get details of every user that logs into our
When a user logs in with his UserName , I send the UserName to
Once a user logs into their account, they are presented with a list of
Hey guys I'm running into a weird issue.. Basically, a user logs into a
Below I have 3 php scripts going in order from when user logs in,
Scenario: A user logs into a site (like StackOverflow) with an OpenID. A year

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.