Establishment of Flask user sessions



  • Using python Flask for the web application. I can't handle sessions. There's been a lot of examples, but there's no clear use in the head. For example, what code is written to establish sessions.

    def login():
       if request.method == 'POST':
          session['username'] = request.form['username']
          return redirect(url_for('index'))
       return '''
    

    @app.route('/logout')
    def logout():
    # remove the username from the session if it is there
    session.pop('username', None)
    return redirect(url_for('index'))

    The next question. One user understands everything, and when there's enough to take it from me. 'username'in order to complete the session for the user of the logout On the website?



  • Each user has its own session, which stores data relating only to it.

    If it is sufficiently simplified, the sessions can be presented in the form of a dictionary where the key will be some user identifier that sends us the browser and the meaning of the session:

    {
        'aa04c3e7-c9f8-4d6f-b859-0162cf5b5af4': {
            'id': 20,
            'last_login': 1478351885
            'username': 'Aleksey'
        },
        '5253b4fe-cf1d-4b76-8d73-e125457d8cf7': {
            'id': 21,
            'last_login': 1478323910
            'username': 'Ivan'
        },
        'f1e7f9a8-858c-4613-994d-340022420d62': {
            'id': 22,
            'last_login': 1478351918
        }
    }
    
    1. User's moving on. /logout
    2. Flask receives the user session identifier from the browser and transmits it to the meeting manager. For example: 5253b4fe-cf1d-4b76-8d73-e125457d8cf7
    3. The meeting manager shall, on the basis of the identifier received, identify the incoming user with id = 21 and shall make the data of its session at the facility sessionwhich will be available in the function/view logout():

    session = { 'id': 21, 'last_login': 1478323910, 'username': 'Ivan' }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2