-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
74 lines (55 loc) · 2.39 KB
/
app.py
File metadata and controls
74 lines (55 loc) · 2.39 KB
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
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, render_template, request,redirect,flash
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///employee.db" # initialising sqlacheemy connectiong it woth my database and creting a database name employee
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config['SECRET_KEY'] = 'supersecretkey'
db = SQLAlchemy(app)
app.app_context().push() #written to create a database
class Employee(db.Model): #model here works beacuse flask works on MVC - model view controller
sno = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(200), nullable = False)
email = db.Column(db.String(500), nullable = False)
@app.route("/",methods=['GET','POST'])
def home():
if request.method == 'POST':
name=request.form.get('name','').strip()
email=request.form.get('email','').strip()
if not name or not email:
flash("all fields ar required", "danger")
return redirect("/")
employee = Employee(name = name, email = email)
db.session.add(employee)
db.session.commit()
flash("form submitted successfully", "Success")
return redirect("/")
allemployee=Employee.query.all()
return render_template("index.html",allemployee=allemployee)
@app.route("/delete/<int:sno>")
def delete(sno):
employee = Employee.query.filter_by(sno=sno).first()
db.session.delete(employee)
db.session.commit()
return redirect("/")
@app.route("/about")
def about():
return render_template('about.html')
#return "<p>This is my about page</p>"
@app.route("/update/<int:sno>", methods=['GET', 'POST'])
def update(sno):
employee = Employee.query.filter_by(sno=sno).first()
if request.method == 'POST':
name = request.form.get('name', '').strip()
email = request.form.get('email', '').strip()
if not name or not email:
flash("All fields are required", "danger")
return redirect(f"/update/{sno}")
employee.name = name
employee.email = email
db.session.commit() # no need for db.session.add()
flash("Employee updated successfully", "success")
return redirect("/")
return render_template("update.html", employee=employee)
if __name__ =='__main__':
app.run(debug=True)