Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @app.route('/', methods=['GET', 'POST'])
- def login():
- # Output message if something goes wrong...
- msg = ''
- # Check if "username" and "password" POST requests exist (user submitted form)
- if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
- # Create variables for easy access
- username = request.form['username']
- password = request.form['password']
- # Check if account exists using MySQL
- #cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
- cursor.execute('SELECT * FROM accounts WHERE username = %s AND password = %s', (username, password,))
- # Fetch one record and return result
- account = cursor.fetchone()
- # If account exists in accounts table in out database
- if account:
- # Create session data, we can access this data in other routes
- session['loggedin'] = True
- session['id'] = account['id']
- session['username'] = account['username']
- # Redirect to home page
- # return 'Logged in successfully!'
- return redirect(url_for('home'))
- else:
- # Account doesnt exist or username/password incorrect
- msg = 'Incorrect username/password!'
- # Show the login form with message (if any)
- return render_template('index.html', msg=msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement