If you’re just starting out with Django, the popular Python web framework, one of the first things you’ll encounter is its built-in support for databases.
By default, Django uses SQLite, a lightweight, file-based database engine that’s perfect for small projects, prototyping, or learning purposes.
In this blog post, we’ll walk through how to set up and use Django with SQLite, step by step.
Before diving into the "how," let’s talk about the "why."
Lightweight and Easy to Set Up: SQLite doesn’t require any additional installation or configuration—it works right out of the box.
Great for Development: It’s ideal for testing ideas, building prototypes, or working on smaller applications where scalability isn’t a concern.
Portable: Since SQLite stores data in a single file, it’s easy to move your project between machines without worrying about complex database setups.
While SQLite isn’t suitable for high-traffic production environments (where PostgreSQL or MySQL might be better), it’s an excellent choice for beginners and small-scale projects.
Related Posts:
How to install Django on Windows
How to install Django on Linux
How to create a Django project
How to Run Your First Django Project
How to Set Up a Virtual Environment for Django
How to Install Django in a Virtual Environment
How to Understand the Django Project Structure
How to Configure Django Settings
How to Use the Django Admin Interface
How to Create and Apply Django Migrations
How to Define Models in Django
How to Create Django Templates
How to Use Static Files in Django
How to Implement Django ModelForms
How to Create Django Class-Based Views
How to Implement Django Function-Based Views
How to Handle User Authentication in Django
How to Create Custom Django User Models
How to Implement Django Celery for Background Tasks
How to Use Django with SQLite Database
How to Connect Django to MySQL
To get started, ensure you have Python installed on your system (Python 3.6 or higher). Then, install Django using pip:
pip install djang
Once Django is installed, verify the installation by running:
django-admin --version
This should display the version of Django installed on your machine.
Next, create a new Django project. Navigate to your desired directory and run:
django-admin startproject myproject
Replace myproject with the name of your project. This command will generate a folder structure like this:
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
The settings.py file contains all the configurations for your project, including database settings.
By default, Django is already configured to use SQLite. Open the settings.py file inside your project folder and locate the DATABASES section:
Here’s what each part means:
'ENGINE': Specifies the database backend (sqlite3 in this case).
'NAME': Defines the path to the SQLite database file. By default, Django creates a file named db.sqlite3 in your project directory.
You don’t need to make any changes here unless you want to rename the database file or store it elsewhere.
Django uses migrations to create and manage database tables based on your models. To initialize the database, run the following commands:
python manage.py migrate
This will apply Django’s default migrations, creating essential tables like auth_user, sessions, and more. After running the command, you should see a new file, db.sqlite3, in your project directory.
Now that your database is set up, let’s create a simple app and define some models.
Create an App:
Run the following command to create a new app within your project:
python manage.py startapp myapp
Replace myapp with the name of your app.
Define a Model:
Open the models.py file in your app folder and define a basic model. For example:
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
published_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
This defines a simple Article model with fields for the title, content, and publication date.
Register the App:
Add your app to the INSTALLED_APPS list in settings.py:
INSTALLED_APPS = [
# Default apps...
'myapp',
]
Run Migrations Again:
After defining your model, create and apply migrations:
python manage.py makemigrations
python manage.py migrate
These commands will update your SQLite database to include a table for the Article model.
Django provides a powerful interactive shell for interacting with your database. Start the shell by running:
python manage.py shell
Once inside the shell, you can create, query, and manipulate database records. For example:
from myapp.models import Article
# Create a new article
article = Article(title="My First Article", content="This is the content of my article.")
article.save()
# Query all articles
articles = Article.objects.all()
for a in articles:
print(a.title, a.published_date)
Exit the shell by typing exit().
To see your data in action, you can create views and templates to display it on a webpage. Here’s a quick example:
Add a View:
In views.py of your app, add:
from django.shortcuts import render
from .models import Article
def article_list(request):
articles = Article.objects.all()
return render(request, 'article_list.html', {'articles': articles})
Create a Template:
Create a folder named templates in your app directory and add a file called article_list.html:
<h1>Articles</h1>
<ul>
{% for article in articles %}
<li>{{ article.title }} - {{ article.published_date }}</li>
{% endfor %}
</ul>
Configure URLs:
Update urls.py in your project folder to include the view:
from django.urls import path
from myapp.views import article_list
urlpatterns = [
path('articles/', article_list, name='article_list'),
]
Run the Server:
Start the development server:
python manage.py runserver
Visit http://127.0.0.1:8000/articles/ in your browser to see the list of articles.
Using Django with SQLite is incredibly straightforward, making it an excellent choice for beginners and small projects. Its simplicity allows you to focus on learning Django’s core concepts without getting bogged down by complex database configurations.
However, as your project grows, you may need to switch to a more robust database like PostgreSQL or MySQL. Fortunately, Django makes this transition seamless—you’ll only need to update the DATABASES setting in settings.py.
Have you tried using Django with SQLite? Or do you have questions about getting started? Let me know in the comments—I’d love to help!