0

I'm pretty new to coding with Python and I am trying to save a users username in a session variable which should be a new entry in the session dictionary. The code I have is

from bottle import route, request, debug, run, template

session={'logins':0}

def checkLogin(name,password):
    if name==password:
        return True
    else:
        return False
@route('/')
def welcome():
        return template('welcome.tpl')

@route('/login')
def login_form():
    return template ('login.tpl')

@route('/checkLogin', method='post')
def login_submit():
    global session
    name = request.forms.get('name')
    password = request.forms.get('password')
    if checkLogin(name, password):
        session['logins']=session['logins']+1
        return template('loggedIn2.tpl',message='Successfully logged in!',success=True,logins=session['logins'])
    else:
        return template('loggedIn2.tpl',message='Unlucky! Try again.',success=False)

@route('/logout')
def logout():
        return template ('login.tpl')
debug(True)
run(reloader=True)      

How would I add the user username to the dictionary so that the username can be displayed on the logged in page?

2
Contributors
1
Reply
18
Views
4 Years
Discussion Span
Last Post by slate
1

How would I add the user username to the dictionary so that the username can be displayed on the logged in page?
You make session["username"]=username in checkLogin if login is successfull.

However. This is not a session. Session is a set of dictionaries that is present for each user (anonym or named), who are currently connected to your application. For example if you have more then one user, you have the same session data for all.

Maybe try:
https://github.com/linsomniac/bottlesession

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.