Skip to content

Commit 2f4048e

Browse files
committed
update
1 parent 4953ec6 commit 2f4048e

File tree

5 files changed

+250
-0
lines changed

5 files changed

+250
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from flask import Flask, render_template, request, redirect, url_for, flash
2+
from flask_sqlalchemy import SQLAlchemy
3+
import os
4+
5+
app = Flask(__name__)
6+
app.secret_key = "Secret Key"
7+
8+
path = os.path.abspath( os.path.dirname(__file__))
9+
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///" + os.path.join(path , 'database.sqlite')
10+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
11+
''' if os isnot to be imported
12+
app = Flask(__name__)
13+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.sqlite3'
14+
'''
15+
db = SQLAlchemy(app)
16+
17+
class Crud(db.Model):
18+
id = db.Column(db.Integer , primary_key = True)
19+
name = db.Column(db.String(100))
20+
email = db.Column(db.String(100))
21+
#phone = db.Column(db.String(100))
22+
23+
def __init__(self, name, email, phone):
24+
self.name = name
25+
self.email = email
26+
#self.phone = phone
27+
28+
29+
30+
@app.route('/')
31+
def index():
32+
all_data = Crud.query.all()
33+
return render_template("index.html", all_data = all_data)
34+
35+
@app.route('/insert', methods = ['POST'])
36+
def insert():
37+
if request.method == 'POST':
38+
name = request.form['name']
39+
email = request.form['email']
40+
phone = request.form['phone']
41+
#check the validity of data
42+
my_data = Crud(name, email, phone)
43+
db.session.add(my_data)
44+
db.session.commit()
45+
46+
flash("student Inserted Successfully")
47+
return redirect(url_for('index'))
48+
49+
@app.route('/update', methods = ['POST'])
50+
def update():
51+
if request.method == "POST":
52+
my_date = Crud.query.get(request.form.get('id'))
53+
my_date.name = request.form['name']
54+
my_date.email = request.form['email']
55+
my_date.phone = request.form['phone']
56+
57+
db.session.commit()
58+
flash("student Updated Successfully")
59+
return redirect(url_for('index'))
60+
61+
@app.route('/delete/<id>/')
62+
def delete(id):
63+
my_data = Crud.query.get(id)
64+
db.session.delete(my_data)
65+
db.session.commit()
66+
67+
flash("student Data Deleted Successfully")
68+
return redirect(url_for('index'))
69+
70+
if __name__ == "__main__":
71+
app.run(debug = True)
72+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
6+
<meta charset="UTF-8">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
9+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
10+
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
11+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
12+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
13+
14+
<title> {% block title %} {% endblock %} </title>
15+
16+
</head>
17+
18+
<body>
19+
20+
{% block body %} {% endblock %}
21+
22+
</body>
23+
24+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends 'base.html' %}
2+
3+
{% block title %} Flask SQLalchemy CRUD operation {% endblock %}
4+
5+
{% block body %}
6+
7+
<div class="jumbotron p-3 bg-dark">
8+
<div class="well text-center text-light">
9+
<h2>AIML6m- Python Flask CRUD Web Application </h2>
10+
</div>
11+
</div>
12+
13+
{% endblock %}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<!-- Inheriting all linked files -->
2+
{% extends 'base.html' %}
3+
{% include 'header.html' %}
4+
5+
<!-- head section -->
6+
{% block title %} Flask CRUD operation {% endblock %}
7+
8+
9+
<!-- Body section -->
10+
{% block body %}
11+
12+
<div class="container">
13+
<div class="row">
14+
<div class="col-md-12">
15+
<div class="jumbotron p-3">
16+
17+
<h3>Manage <b>Students</b> <button type="button" class="btn btn-success float-right" data-toggle="modal" data-target="#mymodal">Add new Student</button> </h3>
18+
19+
{% with messages = get_flashed_messages() %}
20+
{% if messages %}
21+
{% for message in messages %}
22+
23+
<div class="alert alert-success alert-dismissable" role="alert">
24+
<button type="button" class="close" data-dismiss="alert" aria-label="close">
25+
<span aria-hidden="true">
26+
x
27+
</span>
28+
</button> {{message}}
29+
</div>
30+
31+
{% endfor %}
32+
{% endif %}
33+
{%endwith%}
34+
35+
<table class="table table-hover table-dark">
36+
37+
<tr>
38+
<th>ID</th>
39+
<th>Name</th>
40+
<th>Email</th>
41+
<th>Phone</th>
42+
<th>Action</th>
43+
</tr>
44+
45+
{% for row in all_data %}
46+
47+
<tr>
48+
<td>{{row.id}}</td>
49+
<td>{{row.name}}</td>
50+
<td>{{row.email}}</td>
51+
<td>{{row.phone}}</td>
52+
<td>
53+
<a href="/update/{{row.id}}" class="btn btn-info btn-xs" data-toggle="modal" data-target="#modaledit{{row.id}}">Edit</a>
54+
<a href="/delete/{{row.id}}" class="btn btn-danger btn-xs" onclick="return confirm('Are You sure To Delete ?')">Delete</a>
55+
</td>
56+
</tr>
57+
58+
<!-- Model for updating student data -->
59+
<div id="modaledit{{row.id}}" class="modal fade" role="dialog">
60+
<div class="modal-dialog">
61+
<div class="modal-content">
62+
<div class="modal-header">
63+
<h4 class="modal-title"> Update student Data </h4>
64+
</div>
65+
<div class="modal-body">
66+
<form action="{{url_for('update')}}" method="POST">
67+
68+
<div class="form-group">
69+
<label>Name</label>
70+
<input type="hidden" name="id" value="{{row.id}}">
71+
<input type="text" class="form-control" name="name" value="{{row.name}}" required="1">
72+
</div>
73+
74+
<div class="form-group">
75+
<label>Email</label>
76+
<input type="email" class="form-control" name="email" value="{{row.email}}" required="1">
77+
</div>
78+
79+
<div class="form-group">
80+
<label>Phone</label>
81+
<input type="number" class="form-control" name="phone" value="{{row.phone}}" required="1">
82+
</div>
83+
84+
<div class="modal-footer">
85+
<button class="btn btn-info" type="submit"> Update</button>
86+
<button class="btn btn-secondary" data-dismiss="modal">Close</button>
87+
</div>
88+
89+
</form>
90+
</div>
91+
</div>
92+
</div>
93+
</div>
94+
95+
{% endfor %}
96+
97+
</table>
98+
99+
</div>
100+
101+
<!-- Model for adding student -->
102+
<div id="mymodal" class="modal fade" role="dialog">
103+
<div class="modal-dialog">
104+
<div class="modal-content">
105+
<div class="modal-header">
106+
<h4 class="modal-title"> Add student </h4>
107+
</div>
108+
<div class="modal-body">
109+
<form action="{{url_for('insert')}}" method="POST">
110+
111+
<div class="form-group">
112+
<label>Name</label>
113+
<input type="text" class="form-control" name="name" required="1">
114+
</div>
115+
116+
<div class="form-group">
117+
<label>Email</label>
118+
<input type="email" class="form-control" name="email" required="1">
119+
</div>
120+
121+
<div class="form-group">
122+
<label>Phone</label>
123+
<input type="number" class="form-control" name="phone" required="1">
124+
</div>
125+
126+
<div class="modal-footer">
127+
<button class="btn btn-success" type="submit"> Add student </button>
128+
<button class="btn btn-secondary" data-dismiss="modal">Close</button>
129+
</div>
130+
131+
</form>
132+
</div>
133+
</div>
134+
</div>
135+
</div>
136+
137+
</div>
138+
</div>
139+
</div>
140+
141+
{% endblock %}

0 commit comments

Comments
 (0)