Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions app.python
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from flask import Flask,render_template,request,url_for,redirect

import mysql.connector
from mysql.connector import Error
from werkzeug.security import generate_password_hash,check_password_hash
app=Flask(__name__)

#mysql db connection configure
db_config={
'host':'localhost',
'user':'root',
'password':'root',
'database':'student'
}
#helper function to get a db
def get_db_connection():
try:
connection=mysql.connector.connect(**db_config)
return connection
except Error as e:
print("Error while connecting to mysql",e)
return None

@app.route("/")
def Dashboard():
return "<p>Hello ,World</p>"

@app.route("/login",methods=['GET','POST'])
def login():
if request.method =="POST":
username=request.form['username']
password=request.form['password']

conn=get_db_connection()
if conn:
cursor=conn.cursor()
cursor.execute("SELECT*FROM users WHERE username=%s",(username,))
user=cursor.fetchone()
conn.close()
if user and check_password_hash(user[4],password):
return redirect(url_for('Dashboard'))
else:
print("Invalid username or password")

return render_template('login.html')

@app.route('/register',methods=['GET','POST'])
def register():
if request.method=="POST":
username=request.form['username']
address=request.form['address']
student_id=request.form['student_id']
password=request.form['password']
email=request.form['email']
print(password)
print(email)
hash_password=generate_password_hash(password)
print(hash_password)
#connect to db
conn=get_db_connection()
if conn:
cursor=conn.cursor()
cursor.execute("SELECT*FROM users WHERE username=%s",(username,))
existing_user=cursor.fetchone()
if existing_user:
print("username already exists.please choose another one.")
conn.close()
return redirect(url_for('register'))
cursor.execute("insert into users(username,address,student_id,password,email) values(%s,%s,%s,%s,%s)",(username,address,student_id,hash_password,email))
conn.commit()
cursor.close()
conn.close()
return redirect(url_for('login'))
return render_template('register.html')

#dynamic routing
#string route(defauly type)
@app.route('/user/<string:username>')
def show_user(username):
return f"Hello,{username}"

@app.route('/user_id/<int:user_id>')
def show_user_by_id(user_id):
return f'user id is {user_id}'

@app.route('/price/<float:price>')
def show_price(price):
return f'The price is{price}'

@app.route('/path/<path:subpath>')
def show_path(subpath):
return f"The path is {subpath}"


@app.route('/uuid/<uuid:item_id>')
def show_uuid(item_id):
return f"The uuid is {item_id}"





app.run(debug=True)