-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
110 lines (68 loc) · 2.64 KB
/
app.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from flask import Flask, render_template, request, redirect, url_for, flash
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.secret_key = "Secret Key"
#SqlAlchemy Database Configuration With Mysql
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:5271@localhost/seustudents_info'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
#Creating model table for our CRUD database
class Data(db.Model):
id = db.Column(db.String(100), primary_key = True)
name = db.Column(db.String(100))
email = db.Column(db.String(100))
phone = db.Column(db.String(100))
batch = db.Column(db.Integer)
gpa = db.Column(db.Float)
def __init__(self, id, name, email, phone, batch, gpa):
self.id = id
self.name = name
self.email = email
self.phone = phone
self.batch = batch
self.gpa = gpa
#This is the index route where we are going to
#query on all our employee data
@app.route('/')
def Index():
all_data = Data.query.all()
return render_template("index.html", students = all_data)
#this route is for inserting data to mysql database via html forms
@app.route('/insert', methods = ['POST'])
def insert():
if request.method == 'POST':
id = request.form['id']
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
batch = request.form['batch']
gpa = request.form['gpa']
my_data = Data(id, name, email, phone, batch, gpa)
db.session.add(my_data)
db.session.commit()
flash("Student Inserted Successfully")
return redirect(url_for('Index'))
#this is our update route where we are going to update our employee
@app.route('/update', methods = ['GET', 'POST'])
def update():
if request.method == 'POST':
my_data = Data.query.get(request.form.get('id'))
my_data.id = request.form['id']
my_data.name = request.form['name']
my_data.email = request.form['email']
my_data.phone = request.form['phone']
my_data.batch = request.form['batch']
my_data.gpa = request.form['gpa']
db.session.commit()
flash("Student information Updated Successfully")
return redirect(url_for('Index'))
#This route is for deleting our employee
@app.route('/delete/<id>/', methods = ['GET', 'POST'])
def delete(id):
my_data = Data.query.get(id)
db.session.delete(my_data)
db.session.commit()
flash("Student Information Deleted Successfully")
return redirect(url_for('Index'))
if __name__ == "__main__":
app.run(debug=True)