How To Set Up Object Storage with Django


DigitalOcean areas is an item storage space solution, perfect for unstructured information particularly sound, movie, pictures or huge amounts of text. For more information about areas and item storage space, it is possible to read An Introduction to DigitalOcean Spaces.

In this guide, we are addressing how exactly to setup your Django application to work alongside areas.


In purchase to do this guide, you ought to have a things that are few up:

With an server that is initial up and a DigitalOcean area and API key, you are willing to start.

Step 1 — setup a digital Environment

If you’ve gotn’t currently, very first enhance and update your host.

  • sudo apt-get enhance && sudo apt-get upgrade that is-y

Your host should deliver with Python 3, you could run the command that is following make sure that it really is set up:

  • sudo apt-get python3 that is install

Next, install pip to control software applications for Python:

  • sudo apt-get install python3-pip that is-y

Finally, we could install the virtualenv module so we could make use of it setting a programming environment:( up**************)

sudo pip3 virtualenv that is install

For extra guidance and information regarding programmig surroundings, it is possible to learn about installing a digital environment.

Step 2 — Create Django App and Install Dependencies

We’ll now proceed to producing the Django software which will be using our DigitalOcean area.

within the server’s house directory, run the command that is following produce a directory (in this situation, we’ll title it django-apps) to carry the task and demand directory:

  • mkdir django-apps
  • cd django-apps

Within this directory, create a digital environment utilizing the command that is following. We’ll call it env, but it can be called by you anything you need.

You are now able to trigger the environmental surroundings and can get feedback that you’re inside environment by the alteration inside demand line’s prefix.

. env/bin/activate

You will get feedback that you’re inside environment by the alteration inside demand line’s prefix. It shall look something such as this, but will alter dependent on just what directory you're in:

Within the environmental surroundings, install the Django package with pip so we could produce and run a Django software. For more information about Django, read our series that is tutorial on developing.

Then create the task utilizing the command that is following in this situation we’ll call it mysite.

  • django-admin startproject mysite

Next we’ll install Boto 3, that is an AWS SDK for Python that may enable our application to connect to such things as S3, EC2 and DigitalOcean Spaces. Because DigitalOcean Spaces is interoperable with Amazon S3, areas can connect to tools particularly Boto 3 easily. To get more information on the contrast between S3 and areas be sure to review the areas docs.

Another collection which essential for the task is django-storages, that is an accumulation customized storage space backends for Django. We’ll additionally install this with pip.

  • sudo pip install django-storages

You have actually setup your dependencies in the environment of the Django software and tend to be now willing to create fixed and directories that are template

Step 3 — include Directories and Assets

With the environment create with dependencies, it's simple to change to the mysite/mysite directory,

  • cd ~/django-apps/mysite/mysite

Within the mysite/mysite directory, run the commands that are following produce the fixed and template directories.

  • mkdir fixed && mkdir templates

We’ll next create the subdirectories for pictures and CSS to reside in the static directory.

  • mkdir static/img && mkdir static/css

Once you’ve made the directories, we’ll down load a test file that we’ll add to our eventually item storage space. Change to the img directory since we’ll be getting a picture.

  • cd ~/django-apps/mysite/mysite/static/img

Within this directory, we’ll down load the DigitalOcean logo design image utilizing Wget’s wget demand. This might be a commonly utilized GNU system, preinstalled on Ubuntu distros, to recover content from internet servers.

  • wget

You’ll understand production like the ( that is following**************)


Resolving (, Linking to (||:443... linked. HTTP demand delivered, waiting for reaction... 200 okay Size: 1283 (1.3K)
post image
Preserving to: ‘DO_Logo_icon_blue.png’ DO_Logo_icon_blue-6edd7377 100per cent[=====================================>] 1.25K --.-KB/s in 0s 2017-11-05 12:26:24 (9.60 MB/s) - ‘DO_Logo_icon_blue.png’ conserved [1283/1283]

At this aspect, in the event that you operate the demand ls, you’ll observe that a picture called DO_Logo_icon_blue.png now exists inside static/img/ directory.

With these directories create therefore the image are going to be keeping installed to your host, we could proceed to modifying the files connected with our Django software.

Step 4 — Edit CSS and HTML data

We’ll start with modifying the design sheet. You need to transfer to the css directory so we could include a style that is basic for the internet software.

  • cd ~/django-apps/mysite/mysite/static/css

Use nano, or any other text editor of the option, to modify the document.

Once the file starts, include the CSS that is following:(**************)


body {
  margin: 0;
  background-color: #f1f1f1;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;

.container {
  width: 80per cent;
  edge: 1px#ddd that is solid
  background-color: #fff;
  cushioning: 20px;
  margin: 40px automobile;

type {
  margin-bottom: 20px;
  cushioning: 10px;
  edge: 1px solid **********************************************************************************) that is#ff(
  width: 350px;

dining table {
  border-collapse: collapse;
  width: 100per cent;

dining table td,
dining table th {
  edge: 1px#eceeef that is solid
  cushioning: 5px 8px;
  text-align: kept;

dining table thead {
  border-bottom: 2px#eceeef that is solid

Once you might be completed, it is possible to conserve and shut the file. From right here, demand templates directory.

  • cd ~/django-apps/mysite/mysite/templates

We have to start a file called home.html and include HTML engrossed for exactly how our web that is basic app be exhibited. Utilizing nano, available the file therefore it’s prepared for modifying:

Within the document, include the ( that is following**************)


{% load static %}
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>Spaces + Django Tutorial</title>
  <link rel="stylesheet" type="text/css" href="{% static"css/app.css' per cent}">
    <h1>Spaces + Django Tutorial</h1>
    <img src="{% static"img/DO_Logo_icon_blue.png' per cent}">
    <h2>Congratulations, you’re Spaces!< that is using;/h2>

Save and shut the file. The file that is last will update is the file so that it points to your newly created home.html file. We need to move into the directory:( that is following**************)

  • cd ~/django-apps/mysite/mysite

Use nano to modify the file.

You can delete every thing inside file and include the ( that is following**************)

from django.conf.urls import url
from django.views.generic import TemplateView

urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),

With these files create, we could proceed to modifying our file to incorporate it with item storage space.

Step 5 — Change Settings

Now it is time for you to improve your settings file together with your areas qualifications, so we could make use of the web page we’ve setup to show the image.

Keep in your mind that within instance we are hardcoding our qualifications for brevity, but this is simply not safe enough for a manufacturing setup. It is strongly recommended that a package is used by you like Python Decouple something such as to mask your areas qualifications. This package will split the settings parameters from your own supply rule, that is required for a production-grade Django application.

We’ll start with navigating to your location of the settings file.

  • cd ~/django-apps/mysite/mysite

Open the declare modifying, utilizing nano:

Add your host internet protocol address as a permitted host.

ALLOWED_HOSTS = ['your-server-ip']

Then include storages to your set up apps area of the settings register and eliminate django.contrib.admin since we won’t be utilizing that within guide. It will appear to be the next.

# Application meaning


Replace and include the text that is highlighted the TEMPLATES area of the settings file, so the task understands where you should find your house.html file.

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'mysite/templates')],
        'APP_DIRS': Real,
        'OPTIONS': {
            'context_processors': [

Finally, let’s improve your settings at the end associated with file. We’ll be incorporating the next underneath the # fixed files area. Make sure you include your access tips, bucket title, therefore the directory you desire your files to reside. A directory can be added by you through your Spaces interface in-browser. At the time of writing, NYC3 is the region that is only areas at this time are, so will be passed away since the endpoint Address.

# fixed files (CSS, JavaScript, photos)

AWS_ACCESS_KEY_ID = 'your-spaces-access-key'
AWS_SECRET_ACCESS_KEY = 'your-spaces-secret-access-key'
AWS_STORAGE_BUCKET_NAME = 'your-storage-bucket-name'
    'CacheControl': 'max-age=86400',
AWS_LOCATION = 'your-spaces-files-folder'

    os.path.join(BASE_DIR, 'mysite/static'),
STATIC_URL = 'https://%s/%s/' per cent (AWS_S3_ENDPOINT_URL, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

Now our settings file is able to incorporate object storage to our Django app.

Step 6 — Collect Static Data

Now we’ll operate collectstatic and notice that is you’ll being transferred, including the image that we’ve saved in our static directory. It shall get utilized in the areas location that we’ve identified inside settings file.

To attempt, let’s navigate to ~/django-apps/mysite/ :

Within the directory, run the command that is following**************)

  • python collectstatic

You’ll understand output that is following should react yes whenever prompted.


You have actually required to gather files that are static the location location as specified inside settings. This can overwrite files that are existing! Are you currently certain for you to do this? Type 'yes' to carry on, or 'no' to cancel:

Then you’ll see more production letting you know the file happens to be copied to areas.


Copying '/root/django-apps/mysite/mysite/static/css/app.css' 1 file that is static, 1 unmodified.

At this aspect, you pointed them to, with app.css in the css directory, and the DO-Logo_icon_blue-.png( if you return to your bucket from your DigitalOcean Cloud account, you’ll see the css and img directories added to the folder************************) image inside img directory.

Step 7 — testing the applying

With every thing create and our files within our item storage space, we could now test our application by navigating to your web page where our file that is static is offered.

First, let’s make sure that our firewall enables traffic to feed slot 8000 by issuing the command that is following**************)

Now, we could run our host by discussing our server’s internet protocol address and port that is using************************************************************************************).

  • python runserver your-server-ip:8000

In an internet browser, demand http://your-server-ip:8000 to understand consequence of the Django application you’ve developed. You will notice the output that is following your web browser:

DigitalOcean Spaces Django Example App

if you are completed with testing your software, it is possible to press CTRL + C to end the runserver demand. This can get back you to definitely the your development environment.

if you are willing to keep your Python environment, it is possible to run the deactivate demand:

Deactivating your development environment will place you back once again to the terminal demand prompt.


In this guide you've got effectively developed a Django application that serves files from DigitalOcean Spaces. Along the way you’ve discovered files that are about static how exactly to handle fixed files and exactly how to provide them from a cloud solution.

You can carry on researching internet development with Python and Django by reading our series that is tutorial on developing.

Froxlor Installation Guide for beginner

Previous article

How exactly to Install and make use of TensorFlow on Ubuntu 16.04

Next article

You may also like


Leave a Reply

More in DigitalOcean