Profile Application using Python Flask and MySQL

Flask is a lightweight web framework for Python that provides libraries to build web applications quickly. It is a micro framework developed by Armin Ronacher, working on WSGI toolkit and Jinja2 template engines. This tutorial will guide you through creating a profile registration application using Flask and MySQL.

Setting up Virtual Environment

First, install the virtual environment to isolate your project dependencies ?

pip install virtualenv

Create a new project directory and set up the virtual environment ?

mkdir Flask
cd Flask
virtualenv venv

Activate the virtual environment based on your operating system ?

For Linux/macOS:

source venv/bin/activate

For Windows:

venv\Scripts\activate

Installing Required Packages

Install Flask and MySQL connector for Python ?

pip install flask flask-mysqldb

You'll also need to install MySQL Workbench from https://dev.mysql.com/downloads/workbench/ to manage your database.

Creating the Database

Open MySQL Workbench and create a database with the following SQL commands ?

CREATE DATABASE tutorialspointprofile;
USE tutorialspointprofile;

CREATE TABLE IF NOT EXISTS accounts(
    id INT(11) NOT NULL AUTO_INCREMENT,
    email VARCHAR(100) NOT NULL,
    organisation VARCHAR(100) NOT NULL,
    address VARCHAR(100) NOT NULL,
    city VARCHAR(100) NOT NULL,
    state VARCHAR(100) NOT NULL,
    country VARCHAR(100) NOT NULL,
    postalcode VARCHAR(100) NOT NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Creating the Flask Application

Create an app.py file in your Flask directory with the following code ?

from flask import Flask, render_template, request, redirect, url_for, session
from flask_mysqldb import MySQL
import MySQLdb.cursors
import re

app = Flask(__name__)
app.secret_key = 'your-secret-key-here'

# MySQL configurations
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'your-password'
app.config['MYSQL_DB'] = 'tutorialspointprofile'

mysql = MySQL(app)

@app.route('/')
def index():
    return redirect(url_for('register'))

@app.route('/register', methods=['GET', 'POST'])
def register():
    msg = ''
    if request.method == 'POST':
        # Check if all required fields are present
        required_fields = ['email', 'organisation', 'address', 'city', 'state', 'country', 'postalcode']
        
        if all(field in request.form for field in required_fields):
            email = request.form['email']
            organisation = request.form['organisation']
            address = request.form['address']
            city = request.form['city']
            state = request.form['state']
            country = request.form['country']
            postalcode = request.form['postalcode']
            
            cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
            
            # Validate email format
            if not re.match(r'[^@]+@[^@]+\.[^@]+', email):
                msg = 'Invalid email address!'
            else:
                # Insert data into database
                cursor.execute('INSERT INTO accounts VALUES (NULL, %s, %s, %s, %s, %s, %s, %s)', 
                             (email, organisation, address, city, state, country, postalcode))
                mysql.connection.commit()
                msg = 'Successfully registered!'
        else:
            msg = 'Please fill out all fields!'
    
    return render_template('register.html', msg=msg)

if __name__ == "__main__":
    app.run(host="localhost", port=5000, debug=True)

Creating the HTML Template

Create a templates folder in your project directory and add a register.html file ?

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>User Registration</title>
    <style>
        .registercontent {
            max-width: 400px;
            margin: 50px auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .textbox {
            width: 100%;
            padding: 10px;
            margin: 5px 0;
            border: 1px solid #ddd;
            border-radius: 3px;
        }
        .btn {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .msg {
            color: red;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="registercontent">
        <div class="registertop">
            <h1>Register</h1>
        </div>
        
        <div class="registerbottom">
            <form action="{{ url_for('register') }}" method="post" autocomplete="off">
                <div class="msg">{{ msg }}</div>
                
                <input type="email" name="email" placeholder="Enter Your Email ID" 
                       class="textbox" id="email" required>
                
                <input type="text" name="organisation" placeholder="Enter Your Organisation" 
                       class="textbox" id="organisation" required>
                
                <input type="text" name="address" placeholder="Enter Your Address" 
                       class="textbox" id="address" required>
                
                <input type="text" name="city" placeholder="Enter Your City" 
                       class="textbox" id="city" required>
                
                <input type="text" name="state" placeholder="Enter Your State" 
                       class="textbox" id="state" required>
                
                <input type="text" name="country" placeholder="Enter Your Country" 
                       class="textbox" id="country" required>
                
                <input type="text" name="postalcode" placeholder="Enter Your Postal Code" 
                       class="textbox" id="postalcode" required>
                
                <input type="submit" class="btn" value="Register">
            </form>
        </div>
    </div>
</body>
</html>

Running the Application

Start the Flask development server by running ?

python app.py

Open your web browser and navigate to http://localhost:5000 to view the registration form. The data entered through the form will be stored in the MySQL database.

Conclusion

This tutorial demonstrates how to create a simple profile registration system using Flask and MySQL. The application includes form validation, database connectivity, and a clean user interface for collecting user profile information.

Updated on: 2026-03-27T15:45:44+05:30

467 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements