Skip to content

Commit 275490d

Browse files
committed
update
1 parent 27bc2a2 commit 275490d

File tree

7 files changed

+216
-0
lines changed

7 files changed

+216
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Flask-sqlite
2+
Basic Flask User Signup and Login Template with SQLite Database
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# ===========
2+
# Imports
3+
# ===========
4+
from flask import Flask, render_template, request, redirect, url_for, jsonify
5+
import sqlite3 as sql
6+
from flask_cors import CORS, cross_origin
7+
8+
# ===================
9+
# Flask instance
10+
# ===================
11+
app = Flask(__name__)
12+
13+
# ======================
14+
# Allow Cross Origin
15+
# ======================
16+
@app.after_request # blueprint can also be app~~
17+
def after_request(response):
18+
header = response.headers
19+
header['Access-Control-Allow-Origin'] = '*'
20+
return response
21+
22+
23+
# ==================================
24+
# Insert data in database (SIGNUP)
25+
# ==================================
26+
def insertUser(username, email, password, contact):
27+
con = sql.connect("test.db")
28+
cur = con.cursor()
29+
phone = int(contact)
30+
query = ("""INSERT INTO USERS
31+
(username,email,password,contact)
32+
VALUES ('%s','%s','%s',%d)""" %
33+
(username, email, password, phone))
34+
cur.execute(query)
35+
con.commit()
36+
con.close()
37+
38+
39+
# =====================================
40+
# Validating data in database (LOGIN)
41+
# =====================================
42+
def validUser(email, password):
43+
con = sql.connect("test.db")
44+
cur = con.cursor()
45+
query = ("""SELECT * FROM USERS
46+
where email = '%s' and password = '%s'
47+
""" %
48+
(email, password))
49+
cur.execute(query)
50+
data = cur.fetchall()
51+
con.close()
52+
return data
53+
54+
55+
# ===================
56+
# Flask Routing
57+
# ===================
58+
59+
@app.route('/')
60+
def home():
61+
return "go at /signin or /signup"
62+
63+
# Login page
64+
@app.route('/signin/', methods=['GET', 'POST'])
65+
def login():
66+
if request.method == 'POST':
67+
rd = validUser(request.form['email'], request.form['password'])
68+
if rd:
69+
return "Sucessful Login"
70+
else:
71+
return "UnSucessful login"
72+
else:
73+
return render_template('index2.html')
74+
75+
76+
# Signup page
77+
@app.route('/signup/', methods=['GET', 'POST'])
78+
def signup():
79+
if request.method == 'POST':
80+
username = request.form['username']
81+
email = request.form['email']
82+
password = request.form['password']
83+
contact = request.form['contact']
84+
insertUser(username, email, password, contact)
85+
return redirect(url_for('login'))
86+
else:
87+
return render_template('index.html')
88+
89+
# api json
90+
@app.route('/sum', methods=['GET','POST'])
91+
def sum():
92+
sum = 0
93+
a = int(request.args.get('a'))
94+
b = int(request.args.get('b'))
95+
sum = a+b
96+
return jsonify(sum)
97+
98+
99+
# Always at end of file !Important!
100+
if __name__ == '__main__':
101+
app.secret_key = 'SURAJ_SECRET_KEY'
102+
app.debug = True
103+
app.run(host='0.0.0.0', port=5000)
104+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ===========
2+
# Imports
3+
# ===========
4+
import sqlite3 as sql
5+
6+
# ========================
7+
# Connecting to database
8+
# ========================
9+
conn = sql.connect("test.db")
10+
cur = conn.cursor()
11+
12+
# =================
13+
# Query execution
14+
# =================
15+
query = ('''CREATE TABLE USERS2
16+
(username TEXT NOT NULL,
17+
email TEXT Primary key,
18+
password TEXT NOT NULL,
19+
contact INT);''')
20+
cur.execute(query)
21+
22+
# ================================
23+
# Closing connection to database
24+
# ================================
25+
conn.close()

ClassExamples/PJ128_AIML6m_Jan21/flask/Flask-sqlite-UserMgt/templates/demo.html

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html lang="en" >
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Signup Form</title>
7+
8+
9+
10+
<style type="text/css">
11+
form > input{
12+
margin: 10px;
13+
}
14+
</style>
15+
16+
</head>
17+
18+
<body>
19+
20+
<div class="body"></div>
21+
<center style="height: 70%;">
22+
<div class="bo">
23+
<div class="grad"></div>
24+
<div class="header">
25+
<div style="width: auto;">Demo User Mgt</div>
26+
</div>
27+
<br>
28+
<div class="login">
29+
<form action="/signup/" method="post">
30+
<input type="text" placeholder="username" name="username"><br>
31+
<input type="email" placeholder="email" name="email"><br>
32+
<input type="password" placeholder="password" name="password"><br>
33+
<input type="tel" placeholder="contact-no" name="contact"><br>
34+
<input type="submit" value="Signup">
35+
</form>
36+
</div>
37+
</div>
38+
</center>
39+
40+
41+
42+
</body>
43+
44+
</html>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en" >
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Signup Form</title>
7+
8+
<style type="text/css">
9+
form > input{
10+
margin: 10px;
11+
}
12+
</style>
13+
14+
</head>
15+
16+
<body>
17+
18+
<div class="body"></div>
19+
<center style="height: 70%;">
20+
<div class="bo">
21+
<div class="grad"></div>
22+
<div class="header">
23+
<div style="width: auto;">Demo User Mgt</div>
24+
</div>
25+
<br>
26+
<div class="login">
27+
<form action="/signin/" method="post">
28+
<input type="email" placeholder="email" name="email"><br>
29+
<input type="password" placeholder="password" name="password"><br>
30+
<input type="submit" value="Login">
31+
</form>
32+
<a href="{{url_for('signup')}}">Signup</a>
33+
</div>
34+
</div>
35+
</center>
36+
37+
38+
39+
</body>
40+
41+
</html>
Binary file not shown.

0 commit comments

Comments
 (0)