How can I set up object storage with Django?

 

Using cloud server is one of the best solutions for business who are looking for better security, reliability, scalability, and flexibility. And today using a cloud server for your business helps you to manage and handle your website easily. Today cloud server hosting has emerged as a hot trend in the recent year as the modern hosting practices make it more beneficial. One more advantage that you get from using cloud server is that you get fully backup services which grant the ability to restore it at any point in time. And today using cloud hosting service with low installation and maintenance costs, it has become the best option for businesses. Not only that today may IT experts suggest using a cloud server for business.


And today if you are using a cloud server and if you are looking for information on how to set up object storage with Django in Cloud server, then here is the complete information on it.


Follow the below steps to set up object storage with Django in Cloud server:

 

---->> First, you need to set up a virtual environment, and you need to update and upgrade your server


$   sudo apt-get update && sudo apt-get -y upgrade


>>> Now your server should update with Python 3 and to check use the below command


$   sudo apt-get install python3


>>>  Now you need to install pip to manage software packages


$   sudo apt-get install -y python3-pip


>>> Now you can install the virtualenv module and now you can set up a program


  sudo pip3 install virtualenv


---->> Once you are done with that now you need to create Django app and you need to install the dependencies



>>> To create the  Django app use the below command


$   mkdir django-apps

$   cd django-apps


>>> Now within this directory you need to create a new virtual environment, use this command


$   virtualenv env


>>> Now you can activate the environment and you need to change your command line’s prefix.


. env/bin/activate


>>> In here you will receive the feedback that you are in that environment and changing in your command line’s prefix.


( my_evn ) roy@ubuntu : $


>>> Now within this environment, install the Django package with pip


( my_evn ) roy@ubuntu : $  pip install django


>>> Now create a project using the below command


( my_evn ) roy@ubuntu : $   django-admin startproject mysite


>>> Now you have to install Boto 3 and it will allow the application to interact


( my_evn ) roy@ubuntu : $   sudo pip install boto3


>>> In here you need to create another library which will be very crucial for your project is django-storages,


( my_evn ) roy@ubuntu : $   sudo pip install django-storages


---->> Now you need to add directories and assets within that environment with all dependencies so that you can switch to the mysite/mysite directory,


( my_evn ) roy@ubuntu : $   cd ~/django-apps/mysite/mysite


>>> Now within the mysite/mysite directory, run the below command to create static and emplate directories.


( my_evn ) roy@ubuntu : $   mkdir static && mkdir templates


>>> Now you need to create the subdirectories for images and CS


( my_evn ) roy@ubuntu : $   mkdir static/img && mkdir static/css


>>> Now you need to conduct a test and you have to check whether it is working fine or not.  


( my_evn ) roy@ubuntu : $   cd ~/django-

apps/mysite/mysite/static/img


>>> Now in here you we will download the HostingRaja logo using wget command


( my_evn ) roy@ubuntu : $   wget https://image.hostingraja.in/images/logo.jpg


>>> Now you will see this output


Output


Resolving www.hostingraja.in (www.hostingraja.in)... 103.82.34.4, 103.82.35.4

Connecting to www.hostingraja.in (www.hostingraja.in)|103.82.34.4|:443... connected.

HTTP request sent, awaiting response... 200 OK

Length: 1842 (1.8K) [image/png]

Saving to: ‘DO_Logo_icon_blue.png’


DO_Logo_icon_blue-6edd7377 100%[=====================================>]   1.25K --.-KB/s in 0s


2015-04-10 18:48:42 (8.65 MB/s) - ‘DO_Logo_icon_blue.png’ saved [1283/1283]


---->> Now in here you need to edit CSS and HTML files, first you need to edit it in the style sheet and then move into the css directory and we will add the basic style sheet


( my_evn ) roy@ubuntu : $   cd ~/django-apps/mysite/mysite/static/css


>>> In here you need to use the nano or another text editor of your choice, to edit the document.


( my_evn ) roy@ubuntu : $   nano app.css


>>> Now you need to add the below CSS


app.css


body {

 margin: 0;

 background-color: #f1f1f1;

 font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;

}


.container {

 width: 80%;

 border: 1px solid #ddd;

 background-color: #fff;

 padding: 20px;

 margin: 40px auto;

}


form {

 margin-bottom: 20px;

 padding: 10px;

 border: 1px solid #ff9900;

 width: 350px;

}


table {

 border-collapse: collapse;

 width: 100%;

}


table td,

table th {

 border: 1px solid #eceeef;

 padding: 5px 8px;

 text-align: left;

}


table thead {

 border-bottom: 2px solid #eceeef;

}


>>> In here you need to save and close the file and have to navigate to the templates directory


( my_evn ) roy@ubuntu : $   cd ~/django-apps/mysite/mysite/templates


>>> Now you open a file home.html and you need to add HTML into it


( my_evn ) roy@ubuntu : $   nano home.html


>>>  Now in that document add the below commands


home.html

{% load static %}

<!DOCTYPE html>

<html>

<head>

 <meta charset="utf-8">

 <title>Spaces + Django Tutorial</title>

 <link rel="stylesheet" type="text/css" href="/{% static 'css/app.css' %}">

</head>

<body>

 <center>

 <header>

   <h1>Spaces + Django Tutorial</h1>

 </header>

 <main>

   <img src="/{% static 'img/DO_Logo_icon_blue.png' %}">

   <h2>Congratulations, you’re using Spaces!</h2>

 </main>

 </center>

</body>

</html>


>>> Now save that file and this will be updated in urls.py file so that it can be pointed to your newly created home.html file


( my_evn ) roy@ubuntu : $   cd ~/django-apps/mysite/mysite


>>> Now use nano to edit the urls.py file


( my_evn ) roy@ubuntu : $   nano urls.py


>>> And in the process if you want to delete all the file you can even do that, just follow the below commands


urls.py

from django.conf.urls import url

from django.views.generic import TemplateView


urlpatterns = [

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

]


---->> Once done with that now you need to update settings, and in here you need to start by navigating to the location of your settings file


( my_evn ) roy@ubuntu : $   cd ~/django-apps/mysite/mysite


>>> Now open the file to edit with the help of nano


( my_evn ) roy@ubuntu : $   nano settings.py


>>> In here you need to add your server IP as an allowed host.


settings.py

...

ALLOWED_HOSTS = ['your-server-ip']

...


>>> Once after adding the IP now you need to add storages to the installed apps sections of the setting file and you need to remove the django.contrib.admin


settings.py

...

# Application definition


INSTALLED_APPS = [

   'django.contrib.auth',

   'django.contrib.contenttypes',

   'django.contrib.sessions',

   'django.contrib.messages',

   'django.contrib.staticfiles',

   'storages'

]

...


>>> Now in here you need to replace and add the highlighted txt to the TEMPLATES in your settings file so your project  knows where to locate your home.html file.


settings.py

...

TEMPLATES = [

   {

       'BACKEND': 'django.template.backends.django.DjangoTemplates',

       'DIRS': [os.path.join(BASE_DIR, 'mysite/templates')],

       'APP_DIRS': True,

       'OPTIONS': {

           'context_processors': [

               'django.template.context_processors.debug',

               'django.template.context_processors.request',

               'django.contrib.auth.context_processors.auth',

               'django.contrib.messages.context_processors.messages',

           ],

       },

   },

]

...


>>>, At last, you need to update your settings at the bottom of the file. And you need to add your own access keys, bucket name, and the directory


settings.py

...

# Static files (CSS, JavaScript, Images)

# https://docs.djangoproject.com/en/1.11/howto/static-files/


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'

AWS_S3_ENDPOINT_URL = 'https://nyc3.digitaloceanspaces.com'

AWS_S3_OBJECT_PARAMETERS = {

   'CacheControl': 'max-age=86400',

}

AWS_LOCATION = 'your-spaces-files-folder'


STATICFILES_DIRS = [

   os.path.join(BASE_DIR, 'mysite/static'),

]

STATIC_URL = 'https://%s/%s/' % (AWS_S3_ENDPOINT_URL, AWS_LOCATION)

STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'


---->>  Now you need to collect static files, in here we will run collectstatic and you can see that the files are being transferred. And to accomplish this you need to navigate it to ~/django-apps/mysite/


( my_evn ) roy@ubuntu : $   cd ~/django-apps/mysite


>>> In the same directory you need to run the below command


( my_evn ) roy@ubuntu : $   python manage.py collectstatic


>>> After add the above command you wil see the following output and in there you need to respond with yes


Output


You have requested to collect static files at the destination

location as specified in your settings.


This will overwrite existing files!

Are you sure you want to do this?


Type 'yes' to continue, or 'no' to cancel:


>>>  After that you will see some more output where it mentions that your files have been copied to spaces


Output

Copying '/root/django-apps/mysite/mysite/static/css/app.css'


1 static file copied, 1 unmodified.


---->>  Now, at last, you need to test the application, but before that you need to ensure that our firewall will allow traffic only through port 8000


( my_evn ) roy@ubuntu : $   sudo ufw allow 8000


>>> Now you can run your server using the port 8000


( my_evn ) roy@ubuntu : $   python manage.py runserver your-server-ip:8000