- Python for Web Development
-
Overview
- Introduction to Flask and Django
- Setting Up a Flask Application
- Django Models and Migrations
- Routing and URL Handling in Django and Flask
- Forms and User Authentication in Django and Flask
- REST API Development with Flask & Django
- Working with Databases (SQLite, PostgreSQL, MySQL)
- Template Engines (Jinja2 for Flask, Django Templates)
- Deployment of Flask & Django Applications on AWS, GCP, and Heroku
- Security Best Practices for Web Apps
Working with Databases (SQLite, PostgreSQL, MySQL)
Databases are the backbone of web applications, storing and managing data efficiently. In this tutorial, we’ll explore how to use SQLite, PostgreSQL, and MySQL with Flask and Django.
1. Databases in Django
Django comes with built-in ORM (Object-Relational Mapping), which makes it easy to interact with databases. By default, Django uses SQLite, but it also supports PostgreSQL and MySQL.
1.1 Configuring the Database in Django
Using SQLite (Default Database)
Django uses SQLite by default. The configuration in settings.py
looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / "db.sqlite3",
}
}
No extra setup needed. SQLite is great for small projects and local development.
Using PostgreSQL in Django
1. Install the PostgreSQL adapter for Python:
pip install psycopg2-binary
2. Update settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'your_database',
'USER': 'your_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '5432',
}
}
3. Apply migrations and create a superuser:
python manage.py migrate
python manage.py createsuperuser
PostgreSQL is great for large-scale applications needing advanced features like JSONB and full-text search.
Using MySQL in Django
1. Install the MySQL adapter for Python:
pip install mysqlclient
2. Update settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_database',
'USER': 'your_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '3306',
}
}
3. Apply migrations:
python manage.py migrate
MySQL is best for applications that require speed and reliability, such as e-commerce sites.
2. Databases in Flask
Flask is more flexible than Django but requires additional setup for databases.
Using SQLite in Flask
1. Install SQLite support:
pip install flask_sqlalchemy
2. Configure the database in app.py
:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
3. Define a model:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
4. Create the database:
python
>>> from app import db
>>> db.create_all()
SQLite is great for small apps or local development.
Using PostgreSQL in Flask
1. Install PostgreSQL adapter:
pip install psycopg2-binary
2. Configure app.py
:
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://your_user:your_password@localhost/your_database'
3. Apply the same steps as SQLite to create the database.
PostgreSQL is useful for large applications needing scalability and complex queries.
Using MySQL in Flask
1. Install MySQL adapter:
pip install pymysql
2. Configure app.py
:
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://your_user:your_password@localhost/your_database'
3. Apply the same steps as above.
MySQL is ideal for performance-driven applications.
3. Django vs Flask Database Handling
Feature | Django (ORM) | Flask (SQLAlchemy) |
---|---|---|
Ease of Use | Easier, built-in ORM | More flexible, requires setup |
Performance | Optimized for Django | Faster for lightweight apps |
Best for | Full-stack Django apps | Microservices, APIs |
Django's ORM is easier for beginners.
Flask with SQLAlchemy gives more control over queries.
Conclusion
We explored how to use SQLite, PostgreSQL, and MySQL in both Django and Flask.
- SQLite is great for small projects.
- PostgreSQL is best for large-scale applications.
- MySQL is ideal for high-performance applications.
Prepare for Interview
- Debugging in Python
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
- Generators in Python
- Requests in Python
- Django
- Flask
- Matplotlib/Seaborn
- Pandas
- NumPy
- Modules and Packages in Python
- File Handling in Python
- Error Handling and Exceptions in Python
- Indexing and Performance Optimization in SQL
Random Blogs
- Deep Learning (DL): The Core of Modern AI
- Convert RBG Image to Gray Scale Image Using CV2
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- Loan Default Prediction Project Using Machine Learning
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
- AI & Space Exploration – AI’s Role in Deep Space Missions and Planetary Research
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- Role of Digital Marketing Services to Uplift Online business of Company and Beat Its Competitors
- Datasets for Natural Language Processing
- Big Data: The Future of Data-Driven Decision Making
- Variable Assignment in Python
- Datasets for Exploratory Data Analysis for Beginners
- Downlaod Youtube Video in Any Format Using Python Pytube Library
- The Ultimate Guide to Starting a Career in Computer Vision
Datasets for Machine Learning
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset
- Bitcoin Heist Ransomware Address Dataset