Request: If you don't know how to create website in Python using Flask then first visit the below links
Flask is a popular Web Framework which is used to create a website in python. We can also create a website in Python using Django Web Framework, in the upcoming post i will create a complete post on creating a website using Python and Django Framework.
In this post i will explain step by step the following topics:
Let's Start
Create Database in MySQL
Open MySQL in your system and create a database with name "ProjectReporting", If you want to create database using SQL Query then copy the below query and execute this to create database. Following is the query for creating database in MySQL.
CREATE DATABASE ProjectReporting
CREATE TABLE ADMIN ( ADMIN_ID int NOT NULL AUTO_INCREMENT, USERNAME varchar(255) NOT NULL, PASSWORD varchar(255), PRIMARY KEY (ADMIN_ID) );
INSERT INTO TABLE_NAME (column1, column2,column3,...columnN)VALUES (value1, value2, value3,...valueN);
INSERT INTO ADMIN(USERNAME, PASSWORD, EMAIL) VALUES ('admin', 'admin123'), ('krazy', 'programmer'')
<html> <head> <title>KrazyProgrammer</title> <style> a{ color:white; background-color:#FF3399; padding:5px 15px 5px 15px; text-decoration:none; border-radius:20px; display: block; width: 120px; } .login-dark { background-color:rgb(30,40,51); text-align:center; padding:20px; width:300px; height:300px; border-radius:5px; } * {box-sizing: border-box;} body {font-family: Verdana, sans-serif;} .mySlides {display: none;} img {vertical-align: middle;} </style> </head> <body style="background-color:rgb(6,23,58)"> {{error}} <table border="0" width="100%" height="10%" bgcolor="#000000" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;border-bottom:1px dotted white;"> <tr> <td style="color:#ffffff;font-size:50px;text-shadow:2px 2px 2px red">Project Reporting</td> <td width="10%" align="center"> <a href="index">Home</a></td> <td width="10%" align="center"> <a href="about">About</a></td> <td width="10%" align="center"> <a href="projectreg">Registration</a></td> <td width="10%" align="center"> <a href="projectlist">Project List</a></td> <td width="10%" align="center"> <a href="admin">Admin</a></td> </tr> </table><br><br><br><br><br><br><br> <table border="0" bgcolor="#666633" style="box-shadow:1px 1px 10px white" width="70%" height="70%" align="center"> <tr> <td align="center" width="50%"> <img src="static/image/login.jpg" style="width:500px;height:300px;border-radius:5px"> </td> <td style="background-position:center center" align="center"> <div class="login-dark"> <form method="post" action="{{ url_for('admin') }}"> {{msg}} <h2 class="sr-only" style="color:#6666ff">Login Form</h2> <div class="illustration"><i class="icon ion-ios-locked-outline"></i></div> <div class="form-group" style="padding:10px"> <input class="form-control" style="padding:5px" type="text" name="uname" placeholder="Username" required> </div> <div class="form-group" style="padding:10px"> <input class="form-control" style="padding:5px" type="password" name="pwd" placeholder="Password" required></div> <div class="form-group" style="padding:10px"> <button class="btn btn-primary btn-block" style="background-color:rgb(33,74,128);width:169px;border:none;color:white;padding:5px 10px 5px 10px;border-radius:2px;" type="submit">Log In</button></div> </form> </div> </td> </tr> <tr style="background-color:black;color:white;height:25px"> <td colspan="2"><marquee behavior="alternate"> Welcome of You in my Project. </marquee></td> </tr> </table> </body> </html>
from flask import Flask, render_template, request, redirect, url_for, session,flash from flask_mysqldb import MySQL import MySQLdb.cursors app = Flask(__name__) #code for connection with MySQL #MySQL host app.config['MYSQL_HOST'] = 'localhost' #MySQL username app.config['MYSQL_USER'] = 'root' #MySQL password app.config['MYSQL_PASSWORD'] = '' #MySQL Database name app.config['MYSQL_DB'] = 'projectreporting' mysql = MySQL(app) @app.route('/') @app.route('/admin',methods=['GET','POST']) def admin(): msg='' if request.method=='POST' and 'uname' in request.form and 'pwd' in request.form: un=request.form['uname'] pwd=request.form['pwd'] cur=mysql.connection.cursor(MySQLdb.cursors.DictCursor) cur.execute("select * from admin where username=%s and password=%s",(un,pwd,)) acount=cur.fetchone() if acount: msg='Logged in Successfully' #After login if you want to call a new page then use the below line #return render_template("index.html") else: msg='Wrong username or password!' return render_template("admin.html",msg=msg) if __name__ == '__main__': app.run(port=5000,debug=True)
Login with Success
1 Comments
Thanks
ReplyDelete