Skip to content

Integrating Supabase with Django: A Comprehensive Guide

Published: at 08:20 PM

Table of contents

Open Table of contents

Introduction

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. It is free and open source. Django is ridiculously fast, secure, and scalable. Read more…

Supabase is often termed as an opne-source Firebase alternative. Supabase provides a PostgreSQL database, authentication, instant APIs, and real-time subscriptions, making it an excellent backend solution for Django developers. In this guide, we’ll walk through the integration process using best practices in Django.

Why I think Supabase is the Best for Django

  1. PostgreSQL Database
    Supabase is built on PostgreSQL, which Django natively supports. This makes it easy to integrate Supabase’s database into your Django project.

  2. Authentication
    Supabase provides built-in authentication with email/password, social logins, and magic links, eliminating the need to set up your own authentication system.

  3. Instant APIs
    Supabase automatically generates RESTful APIs for your database tables, complementing Django’s ORM for frontend-heavy applications.

  4. Real-Time Functionality
    Supabase offers real-time subscriptions, which Django lacks out of the box. This is perfect for building real-time features like chat apps or live notifications.

  5. Scalability
    Supabase is designed to scale effortlessly, making it ideal for Django applications that need to handle growing traffic and data.

Prerequisites

One would ask, is there any prerequisites? Ofcourse, there is.

Step 1: Setting Up Your Django Project

django-admin startproject myproject
cd myproject
python manage.py startapp myapp
INSTALLED_APPS = [
    ...
    'myapp',
]

Step 2: Setting Up Supabase

  1. Create a New Project in Supabase:

    • Log in to your Supabase dashboard.

    • Click on ‘New Project.’

    • Fill in the project details and wait for the database to be provisioned.

    • Click on connect to get your database uri or see db config details.

  2. Get Your Project URL and API Key:

    • Navigate to the “Settings” section in your Supabase dashboard.

    • Under the “API” tab, find your Project URL and anon (public) key.

Step 3: envuring Django to Use Supabase

SUPABASE_URL=your-supabase-project-url
SUPABASE_KEY=your-supabase-anon-key
DATABASE_URI=database-uri
import environ
import dj_database_url

env = environ.Env()
environ.Env.read_env()

DATABASES = {
    'default': dj_database_url.config(default=env('DATABASE_URI'))
}

SUPABASE_URL = env('SUPABASE_URL')
SUPABASE_KEY = env('SUPABASE_KEY')
pip install supabase

Step 4: Using Supabase in Your Django App

from supabase import create_client, Client
from django.conf import settings

def get_supabase_client() -> Client:
    return create_client(settings.SUPABASE_URL, settings.SUPABASE_KEY)

def fetch_todos():
    supabase = get_supabase_client()
    response = supabase.table('todos').select("*").execute()
    return response.data
from django.shortcuts import render
from .services.supabase_service import fetch_todos

def todo_list(request):
    todos = fetch_todos()
    return render(request, 'myapp/todo_list.html', {'todos': todos})
<h1>Todo List</h1>
<ul>
  {% for todo in todos %}
  <li>
    {{ todo.task }} - {% if todo.completed %}Done{% else %}Not Done{% endif %}
  </li>
  {% endfor %}
</ul>
from django.urls import path
from . import views

urlpatterns = [
    path('todos/', views.todo_list, name='todo_list'),
]
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
]

Step 5: Making migrations

python migrate

Migrations

Step 5: Running Your Django Server

python manage.py runserver
http://127.0.0.1:8000/todos/

Data in DB

You can as well django shell to interact with your models.

python manage.py shell

Push Todo

Conclusion

The Look

You just did it buddy!

References

  1. Supabase
  2. Django
  3. Link to Code