-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
40 lines (31 loc) · 1.26 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///aaldb.db' # SQLite database, change as needed
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
firstname = db.Column(db.String(50), nullable=False)
lastname = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
role = db.Column(db.String(50), nullable=False)
@app.route('/')
def index():
users = User.query.all()
return render_template('index.html', users=users)
@app.route('/add-user', methods=['GET', 'POST'])
def add_user():
if request.method == 'POST':
firstname = request.form['firstname']
lastname = request.form['lastname']
email = request.form['email']
role = request.form['role']
new_user = User(firstname=firstname, lastname=lastname, email=email, role=role)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('index'))
return render_template('add-user.html')
if __name__ == '__main__':
db.create_all()
app.run(debug=True)