-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
42 lines (33 loc) · 1.28 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
from flask import Flask, render_template, request, redirect, url_for
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config["MYSQL_HOST"] = "localhost"
app.config["MYSQL_USER"] = "root"
app.config["MYSQL_DB"] = "py_db"
app.config["MYSQL_CURSORCLASS"] = "DictCursor"
mysql = MySQL(app)
@app.route("/")
def home():
con = mysql.connection.cursor()
sql = "SELECT * FROM users"
con.execute(sql)
res = con.fetchall()
return render_template("index.html", datas=res)
# New User Creation
@app.route('/addusers', methods=['GET', 'POST'])
def addusers():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
sys_username = request.form['sys_username']
sys_ip = request.form['sys_ip']
sys_password = request.form['sys_password']
con = mysql.connection.cursor()
sql = "INSERT INTO users (name, email, sys_username, sys_ip, sys_password) VALUES (%s, %s, %s, %s, %s)"
val = (name, email, sys_username, sys_ip, sys_password)
con.execute(sql, val)
mysql.connection.commit()
return redirect(url_for('home')) # Redirect to home page after adding user
return render_template("addusers.html")
if __name__ == "__main__":
app.run(debug=True)