Although I do not have a role system setup, here is a basic example that I was playing around with a while back.
Hope it helps.
I installed gunicorn in the venv and it ran fine. On a side note you can also use pkill gunicorn to kill the server
database.py
error.html
Hope it helps.
I installed gunicorn in the venv and it ran fine. On a side note you can also use pkill gunicorn to kill the server
database.py
import sqlite3 class Database: def __init__(self): self.connection = sqlite3.connect('users.db') self.cursor = self.connection.cursor() def getuser(self, user, passwd): query = f'select user, password from users where user="{user}" and password="{passwd}"' return self.cursor.execute(query).fetchone()app.py
import sqlite3 from flask import (Flask , render_template, url_for, request, flash, redirect, session) from database import Database app = Flask(__name__) app.secret_key = 'my secret key' @app.route('/') def index(): return render_template('index.html') @app.route('/form') def form(): return render_template('form.html') @app.route('/validate', methods=['POST', 'GET']) def validate(): if request.method == 'POST': user = request.form['user'] passwd = request.form['mypass'] db = Database() data = db.getuser(user, passwd) if data != None: if data[0] == user.lower() and data[1] == passwd: session['user'] = data[0] session['password'] = data[1] session['role'] = data[2] flash(f'Hello {data[0]}') return redirect(url_for('success')) flash(f'Sorry, username, password combination does not exist.') return redirect(url_for('error')) @app.route('/error') def error(): return render_template('error.html') @app.route('/success') def success(): return render_template('success.html') @app.route('/logout') def logout(): session.pop('user', None) session.pop('password', None) session.pop('role', None) return redirect(url_for('index')) if __name__ == '__main__': app.run(debug=True)In templates folder
error.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> {% with messages = get_flashed_messages() %} {% if messages %} {% for message in messages %} <p style="background-color: orange; color: navy; padding:8px;">{{ message }}</p> {% endfor %} {% endif %} {% endwith %} <a href="{{url_for('form')}}">Login</a><br /> <a href="{{url_for('index')}}">Index</a> </body> </html>form.html
<form action="{{url_for('validate')}}" method="post"> <table> <tr> <td>User:</td><td><input type="text", name="user"></td> </tr> <tr> <td>Password:</td><td><input type="password" name="mypass"></td> </tr> <tr> <td><input type="submit" value="Submit"></td> </tr> </table> </form>index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>FlaskBlog</title> </head> <body> <h1>Welcome to Flask</h1> <a href="{{url_for('form')}}">Login</a> </body> </html>success.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1>Success Page</h1> {% with messages = get_flashed_messages() %} {% if messages %} {% for message in messages %} <p style="background-color: orange; color: navy; padding: 8px;">{{ message }}</p> {% endfor %} {% endif %} {% endwith %} <a href="{{url_for('index')}}">Logout</a> </body> </html>
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags