DEV Community

Cover image for Django + Firebase Cloud Storage
Muhammad Rahiman
Muhammad Rahiman

Posted on

Django + Firebase Cloud Storage

This is a sample project to test Firebase storage integration with Django using Pyrebase package.

Repository link:

Getting Started

1. Set up your Django project

If you are cloning this repo, run the following command preferably inside your virtual environment and skip to step 5:

  • Using pipenv:

    $ pipenv install -r requirements.txt # (Python 2)
    $ pipenv3 install -r requirements.txt # (Python 3)
  • Using venv:

    $ pip install -r requirements.txt # (Python 2)
    $ pip3 install -r requirements.txt # (Python 3)

Else, to create your Django project from scratch (make sure to have Django installed):

$ django-admin startproject project_name
Enter fullscreen mode Exit fullscreen mode

Next, navigate into the newly created project folder. Then, start a new Django app. We will also run migrations and start up the server:

$ cd project_name
$ python startapp app_name
$ python migrate
$ python runserver
Enter fullscreen mode Exit fullscreen mode

If everything works well, we should see an instance of a Django application running on this address — http://localhost:8000

alt text

2. Configure project settings

  1. Add app inside INSTALLED_APPS (

    Once you’ve created the app, you need to install it in your project. In project_name/, add the following line of code under INSTALLED_APPS:


    That line of code means that your project now knows that the app you just created exists.

  2. Add templates folder directory in TEMPLATES (project_name/

            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': ['templates/'], # HERE
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
  3. Add static and media folder directory in STATIC_ROOT (project_name/

    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  4. Add desired URL for the app (project_name/

    from django.contrib import admin
    from django.urls import path, include
    urlpatterns = [
        path('(INSERT_URL)', include('APP_NAME.urls')),
  5. Create new for the app (app_name/

3. Configure app settings

  1. Create new template (app_name/templates/)

    • You need to create the HTML template to display to the user after creating a view function.
    • Create a directory named templates and subsequently a file named app_name.html inside it:

      # Create directory (if haven't)
      mkdir app_name/templates/
      # Create HTML template
      touch app_name/templates/app_name.html
  2. Create view function (FBV/CBV) in app's

    • Views in Django are a collection of functions or classes inside the file in your app directory.
    • Each function or class handles the logic that gets processed each time a different URL is visited.

      from django.shortcuts import render
      def view_name(request):
          return render(request, 'template_name.html', {})
- The function defined is called a **view function**. When this function is called, it will render an HTML file called `app_name.html`.
Enter fullscreen mode Exit fullscreen mode
  1. Add URL to app's

    • The final step is to hook up your URLs so that you can visit the page you’ve just created.
    • Your project has a module called in which you need to include a URL configuration for the app. Inside app_name/, add the following:

      from django.contrib import admin
      from django.urls import path, include
      urlpatterns = [
          path('', views.view_name, name="view_name"),
      • This looks for a module called inside the hello_world application and registers any URLs defined there.
      • Whenever you visit the root path of your URL (localhost:8000), the application’s URLs will be registered.

4. Install Pyrebase package

Pyrebase is a simple python wrapper for the Firebase API.

To install Pyrebase:

$ pip install pyrebase
Enter fullscreen mode Exit fullscreen mode

5. Create a new Firebase project

In order to use Firebase, you need to be logged into your Google account. Go to Firebase and click Get started. You will be redirected to Firebase Console.

Next, click Add project and fill up the necessary fields.

Imgur Image

6. Create Firebase app

Once the project has been successfully created, click the '</>' button to create a new Firebase web app.

Imgur Image

Go through the 'Add Firebase to your web app' creation process as shown below.

Imgur Image

When you reach the 'Add Firebase SDK' part, copy the web app's Firebase configuration as highlighted in the image below. You will need this for your Pyrebase config inside Django later.

Imgur Image

7. Set up Django app views

The next step is to initialise our Firebase connection in Python (Django) by first importing the pyrebase package and creating a dictionary called config based on the Firebase configuration that you copied previously. You will need to amend the codes a bit by encapsulating the keys as strings.

You also need to add another key-value pair inside the dictionary called databaseURL. This is because in order to connect with Firebase, you need to declare the databaseURL environment variable (which in this guide is not configured since we will be focusing more on Firebase Cloud Storage).

# app_name/
import pyrebase
import os

config = {
    "apiKey": "KEY",
    "authDomain": "",
    "projectId": "project_name-id",
    "storageBucket": "",
    "messagingSenderId": "msg_sender_id",
    "appId": "1:app_id",
    "measurementId": "G-measurement_id",
    "databaseURL": ""
Enter fullscreen mode Exit fullscreen mode

Then, to initialise the Firebase connection and storage, write the following codes:

# app_name/

firebase = pyrebase.initialize_app(config)
storage =
Enter fullscreen mode Exit fullscreen mode

8. Create new Firebase Cloud Storage

To create a Cloud Storage, click 'Storage' on the left sidebar and then click 'Get Started' button.

Imgur Image

Follow the two steps prompted. Keep in mind that the Cloud Storage location should be in your region (in our case, southeastasia2).

Imgur Image

Once the Cloud Storage has been successfully created, go to the Rules tab and edit the rules to the following (IMPORTANT: only for development purposes!)

Imgur Image

rules_version = '2';
service {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write
Enter fullscreen mode Exit fullscreen mode

9. Configure file upload to Firebase Cloud Storage

Going back to app_name/, to upload files inside the Firebase Cloud Storage, we need to use the storage.child() and storage.put() functions as follows:

# app_name/

storage =

# Example (same directory, same file name)

# Example (different directory, different file name)
Enter fullscreen mode Exit fullscreen mode

The child() method is used to build paths to your data with the Storage service. You can specify the file directory and even the uploaded file name (renaming). The put() method on the other hand takes the path to the local file that you want to upload on Cloud Storage.

10. Implement file upload logic (optional)

This is an additional and optional step on my part, where I created a simple file upload logic from the template form:

# app_name/

from django.shortcuts import render, redirect
from django.shortcuts import render
from django.conf import settings
from import default_storage
from django.contrib import messages
import pyrebase
import os

config = {
    "apiKey": "AIzaSyA-99K3f5TK26Z7n5szV4wLq_fl6Gf9kcs",
    "authDomain": "",
    "projectId": "dj-firebase-6f10d",
    "storageBucket": "",
    "messagingSenderId": "719069501836",
    "appId": "1:719069501836:web:822c7843a3d428d32f9ea9",
    "measurementId": "G-VLZXVGDCZF",
    "databaseURL": ""

firebase = pyrebase.initialize_app(config)
storage =

def main(request):
    if request.method == 'POST':
        file = request.FILES['file']
        file_save =, file)
        storage.child("files/" +"media/" +
        delete = default_storage.delete(
        messages.success(request, "File upload in Firebase Storage successful")
        return redirect('main')
        return render(request, 'main.html')

Enter fullscreen mode Exit fullscreen mode

What this essentially does is that the user is able to upload any file via the form created using TailwindCSS. The function-based view will then retrieve the uploaded file, save it locally (MEDIA_ROOT directory as specified in and using Django's default_storage library), upload the file in Firebase Cloud Storage and then delete the local file (since I only cared about uploading in cloud instead of local).



Top comments (4)

junchao1507 profile image
Jun Chao Thean • Edited

Thanksss! This really helps!

artu_hnrq profile image
Arthur Henrique

Looking forward on how to use Firebase Storage directly through Django's storage system

modexanderson profile image

How do i upload to firebase cloud storage from django admin panel please?

divuzki profile image
Divine Ikhuoria

you can add it to your at the def save(): function