Welcome folks today in this blog post we will be inserting user profile
into mysql database
using html5 form
in browser using flask
framework of python .All the full source code of the application will be given below.
Get Started
In order to get started you need to install the below libraries using the pip
command as shown below
pip install flask
pip install flask_mysqldb
And after that you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from flask import Flask,render_template,request from flask_mysqldb import MySQL app = Flask(__name__) app.config['MYSQL_HOST'] = "localhost" app.config['MYSQL_USER'] = "root" app.config['MYSQL_PASSWORD'] = "" app.config['MYSQL_DB'] = "users_db" if __name__ == "__main__": app.run(debug=True) |
As you can see we are importing the mysql
and flask library and then we are configuring the hostname
username and password and also the database name to connect to the database.
Now we need to show the user
form when user visit the homepage
of the flask app as shown below
1 2 3 |
@app.route('/',methods=['GET','POST']) def index(): return render_template('index.html') |
As you can see we are loading the index.html
template using the render_template()
method and now we need to create the templates
folder and create the index.html
file and copy paste the following code
templates/index.html
1 2 3 4 5 6 7 |
<h1>Simple Login Form</h1> <form method="post" action="/"> <input type="text" placeholder="Enter Username" name="username" required> <input type="email" placeholder="Enter Email" name="email" required> <input type="submit" value="Register"> </form> |
As you can see we have two fields which is the username and the email. And we have also a register button to save the information inside the mysql database
Now we need to go to mysql
database and create a new one and table as well
And now we need to write the post
request to save
the information inside the mysql database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@app.route('/',methods=['GET','POST']) def index(): if request.method == "POST": username = request.form['username'] email = request.form['email'] cur = mysql.connection.cursor() cur.execute("INSERT INTO users (username,email) VALUES(%s,%s)",(username,email)) mysql.connection.commit() cur.close() return "Data inserted" return render_template('index.html') |
As you can see we are having a simple INSERT
sql query to insert the user information which is the username and the email to the users table and then we are returning a simple
response to the client that is data is inserted as shown below
And now we need to define get
request to the /users
route where we will be showing all the users inside the table.
1 2 3 4 5 6 7 8 9 10 |
@app.route('/users') def users(): cur = mysql.connection.cursor() users = cur.execute("SELECT * FROM users") if users > 0: userDetails = cur.fetchall() return render_template('users.html',userDetails=userDetails) |
As you can see we are using the select
statement to fetch all records from the table and then we are passing that data to the users.html
template using the render_template()
method
Now we need to create the users.html
file inside the templates
folder and copy paste the following code
templates/users.html
1 2 3 4 5 6 7 8 9 10 11 12 |
<table border="1"> <tr> <th>Username</th> <th>Email</th> </tr> {% for user in userDetails %} <tr> <td>{{user[1]}}</td> <td>{{user[2]}}</td> </tr> {% endfor %} </table> |
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 43 44 |
from flask import Flask,render_template,request from flask_mysqldb import MySQL app = Flask(__name__) app.config['MYSQL_HOST'] = "localhost" app.config['MYSQL_USER'] = "root" app.config['MYSQL_PASSWORD'] = "" app.config['MYSQL_DB'] = "users_db" mysql = MySQL(app) @app.route('/',methods=['GET','POST']) def index(): if request.method == "POST": username = request.form['username'] email = request.form['email'] cur = mysql.connection.cursor() cur.execute("INSERT INTO users (username,email) VALUES(%s,%s)",(username,email)) mysql.connection.commit() cur.close() return "Data inserted" return render_template('index.html') @app.route('/users') def users(): cur = mysql.connection.cursor() users = cur.execute("SELECT * FROM users") if users > 0: userDetails = cur.fetchall() return render_template('users.html',userDetails=userDetails) if __name__ == "__main__": app.run(debug=True) |