├── FlaskPro.zip ├── app.py ├── static └── style.css └── templates ├── addUsers.html ├── editUser.html └── home.html /FlaskPro.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TutorJoes-Stanley/Python_Flask_CRUD_MySQL/d06529b7b6150435d44e605f2f1c1c3131c7c01f/FlaskPro.zip -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask,render_template,url_for,redirect,request,flash 2 | from flask_mysqldb import MySQL 3 | 4 | app=Flask(__name__) 5 | #MYSQL CONNECTION 6 | app.config["MYSQL_HOST"]="localhost" 7 | app.config["MYSQL_USER"]="root" 8 | app.config["MYSQL_PASSWORD"]="root" 9 | app.config["MYSQL_DB"]="crud" 10 | app.config["MYSQL_CURSORCLASS"]="DictCursor" 11 | mysql=MySQL(app) 12 | 13 | 14 | #Loading Home Page 15 | @app.route("/") 16 | def home(): 17 | con=mysql.connection.cursor() 18 | sql="SELECT * FROM users" 19 | con.execute(sql) 20 | res=con.fetchall() 21 | return render_template("home.html",datas=res) 22 | 23 | #New User 24 | @app.route("/addUsers",methods=['GET','POST']) 25 | def addUsers(): 26 | if request.method=='POST': 27 | name=request.form['name'] 28 | city=request.form['city'] 29 | age=request.form['age'] 30 | con=mysql.connection.cursor() 31 | sql="insert into users(NAME,CITY,AGE) value (%s,%s,%s)" 32 | con.execute(sql,[name,city,age]) 33 | mysql.connection.commit() 34 | con.close() 35 | flash('User Details Added') 36 | return redirect(url_for("home")) 37 | return render_template("addUsers.html") 38 | #update User 39 | @app.route("/editUser/",methods=['GET','POST']) 40 | 41 | def editUser(id): 42 | con=mysql.connection.cursor() 43 | if request.method=='POST': 44 | name=request.form['name'] 45 | city=request.form['city'] 46 | age=request.form['age'] 47 | sql="update users set NAME=%s,CITY=%s,AGE=%s where ID=%s" 48 | con.execute(sql,[name,city,age,id]) 49 | mysql.connection.commit() 50 | con.close() 51 | flash('User Detail Updated') 52 | return redirect(url_for("home")) 53 | con=mysql.connection.cursor() 54 | 55 | sql="select * from users where ID=%s" 56 | con.execute(sql,[id]) 57 | res=con.fetchone() 58 | return render_template("editUser.html",datas=res) 59 | #Delete User 60 | @app.route("/deleteUser/",methods=['GET','POST']) 61 | def deleteUser(id): 62 | con=mysql.connection.cursor() 63 | sql="delete from users where ID=%s" 64 | con.execute(sql,id) 65 | mysql.connection.commit() 66 | con.close() 67 | flash('User Details Deleted') 68 | return redirect(url_for("home")) 69 | 70 | if(__name__=='__main__'): 71 | app.secret_key="abc123" 72 | app.run(debug=True) 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /static/style.css: -------------------------------------------------------------------------------- 1 | *{ 2 | margin:0px; 3 | padding:0px; 4 | } 5 | html{ 6 | background:#0984e3; 7 | } 8 | body{ 9 | background:white; 10 | width:800px; 11 | height:1000px; 12 | margin:0 auto; 13 | padding:15px; 14 | font-family:roboto; 15 | color:#2d3436; 16 | } 17 | h1{ 18 | text-align:center; 19 | border-bottom:1px solid grey; 20 | font-family:Galada; 21 | padding:20px; 22 | margin-bottom:25px; 23 | color:#6c5ce7; 24 | } 25 | table{ 26 | width:100%; 27 | border-collapse:collapse; 28 | margin-top:25px; 29 | } 30 | tr td,tr th{ 31 | border:1px solid #00b894; 32 | padding:10px; 33 | } 34 | tr th{ 35 | border:none; 36 | background:#00b894; 37 | border:1px solid #00b894; 38 | color:white; 39 | } 40 | a,input[type=submit]{ 41 | padding:8px; 42 | color:white; 43 | background:grey; 44 | border-radius:5px; 45 | border:none; 46 | } 47 | input[type=submit]{ 48 | margin-top:15px; 49 | background:#00b894; 50 | font-size:15px; 51 | } 52 | input[type=text]{ 53 | border:1px solid grey; 54 | height:30px; 55 | width:300px; 56 | display:block; 57 | margin-top:15px; 58 | font-size:15px; 59 | padding:5px; 60 | } 61 | 62 | label{ 63 | display:block; 64 | margin-top:15px; 65 | font-size:15px; 66 | color:blue; 67 | font-weight:bold; 68 | } 69 | p a{ 70 | background:#2d3436; 71 | text-decoration:none; 72 | } 73 | 74 | 75 | table td:nth-child(5) a{ 76 | background:#e84393; 77 | text-decoration:none; 78 | padding:5px 30px 5px 30px ; 79 | } 80 | table td:nth-child(6) a{ 81 | background:#d63031; 82 | text-decoration:none; 83 | padding:5px 30px 5px 30px ; 84 | } 85 | h4{ 86 | text-align:right; 87 | color:#6c5ce7; 88 | } -------------------------------------------------------------------------------- /templates/addUsers.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Tutor Joes 4 | 5 | 6 | 7 |

Add New User Details

8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | -------------------------------------------------------------------------------- /templates/editUser.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Tutor Joes 4 | 5 | 6 | 7 |

Update User Details

8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | -------------------------------------------------------------------------------- /templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Tutor Joes Flask Tutorial 4 | 5 | 6 | 7 |

User Managment Using Python Flask & MySQL

8 | 9 |

10 | {% with messages = get_flashed_messages() %} 11 | {% if messages %} 12 | {% for message in messages %} 13 | {{ message }} 14 | {% endfor %} 15 | {% endif %} 16 | {% endwith %} 17 |

18 | 19 |

Add Users

20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | {% for data in datas %} 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | {% endfor %} 39 |
IDNAMEAGECITYEDITDELETE
{{data.ID}}{{data.NAME}}{{data.AGE}}{{data.CITY}}EditDelete
40 | 41 | --------------------------------------------------------------------------------