In this guest post, Richard Ackon - a programmer based in Ghana - shares how he got started using Divio Cloud.
Daniele Procida
Community Manager
If you think it’s easy to deploy Django applications with services such as Heroku or AWS, then what I’m going to show you in this post will give you a whole new perspective!
In this post, I’ll introduce you to Divio, a platform that optimises hosting of Django applications and provides you a range of developer tools to make your development and deployment of your Django applications a breeze.
Let’s get started!
I assume that you know how to build a Django application and have a working knowledge of Django web development. The Divio platform makes use of containers so you may wish to familiarise yourself with some Docker basics and specifically how to start and stop containers.
I’ll take a very basic one-page Django site, currently running on Heroku, and show you how you can easily develop and deploy it using Divio.
Here is the code and structure of the one-page application running. All it does is ask you for your name and welcomes you to Divio after you enter your name. It then saves your name into a Postgres database.
If you’ve deployed Django before then you’ll understand that creating a project on your local environment, setting it up to be deployed to a test and production server and versioning your code using a version control software like Git can be quite a long journey.
This is where Divio shines!
Divio allows you to create your project from a clean and intuitive control panel. With Divio, you get your Django project automatically setup with a connection to a Postgres database. This is made possible with the help of Docker. Docker allows you to containerise your application with all its dependencies needed to run it. You can run this container anywhere on any machine and your application will work the same way. This is how Divio is able to help you easily create the same environment for your local, test and live applications.
Let’s dive in and take a look!
You can get started with a free account by signing up directly through the Divio Control Panel.
To manage Divio projects on your local machine, there are a few tools you will want to install. I recommend using the Divio Desktop application to setup and manage this for you. Divio Desktop is available for Windows, Linux and Mac.
Once installed, we can proceed and build something!
Once you have created your account, we will proceed to create a new project.
From the image above, you can see that setting up a a Django project is as easy as typing in the name of your project, selecting the Python version you want and choosing the Project type template (Django, DjangoCMS, Wagtail, Oscar). Once you’ve made your choices, press continue to create your project.
After setting up our project using Control Panel, we can now replicate the project on our local machine so we an edit our project.
We can use either the Divio Desktop application or Divio CLI - the powerful Divio command line which is essentially underlying the Divio Desktop application. In this example, I will use both to show how they work.
Open a fresh terminal and move to a directory where you wish to store and work on your project. You can start by listing the Divio applications you have created.
divio app list
Find your refreshly created project and proceed to replicate it to your machine.
divio app setup <your project name>
Find your refreshly created project and proceed to replicate it to your machine. Replace with the slug of your project after typing the previous command. The setup command unfolds a number of processes and takes a few minutes to complete.
Once this process is complete, cd into the new project directory created and use the following command to start your Django project.
divio app up
Once this process is complete, cd into the new project directory created and use the following command to start your Django project.
Starting up the project opens up a browser window where you’re allowed to add a user to the database before you can log in.
At this point, we have our project setup on our local machine and we also have test and live server setup and ready to host our application.
This is where we build the functionality of our application. I’ll be replicating the one page application that I talked about earlier. Start by creating a new application inside your project.
docker exec <web container name> python manage.py startapp <app_name>
Alternatively, you can copy the mysite app folder from the one page application into your project in the same location as your manage.py file. After creating your application, you are ready to add whatever functionality you want. If you’ve used Django before, the process remains the same.
To add a new application to your installed apps, open the settings.py file add the name of your application in the INSTALLED_APPS.extend section. You can see that your settings.py file is smaller with Divio. This is because Divio manages the rest of the settings for you. Easy right!
You can visit this page for for more information on dealing with the setting.py
INSTALLED_APPS.extend([
# add your project specific apps here
'< YOUR APPLICATION NAME GOES HERE >',
])
Include the urls of your new app in the urls of your project
This is done in the same way you’ll do it for a regular Django project.
urlpatterns = [
# add your own patterns here
url(r'^mysite/', include('mysite.urls', namespace='mysite')),
] + aldryn_addons.urls.patterns() + i18n_patterns(
# add your own i18n patterns here
*aldryn_addons.urls.i18n_patterns() # MUST be the last entry!
)
Migrate your databases
To migrate your database, you’ll have to run the makemigrations and migrate commands inside the running container. Use docker ps
to check the name of the running container and run the following commands to migrate your database.'
docker exec <container name> python manage.py makemigrations
docker exec <container name> python manage.py migrate
Create the necessary views and models
After your database has been migrated, you can go ahead to create the functionality of your application, creating views, models, templates and whatever you need to make your app work.
To verify that your application is working locally as designed, use divio app up
to startup your project. Once your application is working locally, we can now deploy to your test and live servers.
Before we deploy our application to our test as live servers, we first have to commit and push the changes that we’ve made to the project. we do this by using the following commands. Use git status
to check the changes you want to commit and then do the following.
git add <names of files and folders to commit >
or use
git add .
# to add all changes
Now we can commit and push our changes by doing:
git commit -m "Django on Divio"
Finally, push the changes:
git push origin develop
Deploy to your test and live environments
We can easily deploy Divio applications locally with the Divio tools or, with the files now pushed to the Git repo, directly from the Divio Control Panel.
In the image above you can see that we have we have a test and live server setup for us and deploying is as easy as clicking on the big blue Deploy button for either section. You can use the url provided for each server to access your application.
If you’ve read up to this point, you’re probably wondering why you’ve been stressing all this while to build and deploy Django applications. Well, your pain is over now, Divio is here to save the day! Welcome to a new world of easy, full-featured building and deployment of your next Django project!
Richard is a Software Engineer who believes in building products that makes the lives of it’s users easier. Richard is also an avid machine learning practioner who is always looking to make himself and the people around him better. A strong believer in sharing knowledge and ideas, which he does by writing articles, speaking at conferences and teaching local meetup groups.