DEV Community

Cover image for Deploying your project from github using render cloud service
Olaoye kunle
Olaoye kunle

Posted on

Deploying your project from github using render cloud service

Now that you are through with your Django project, it is time to show the world your latest project in django. There are several options you have in order to achieve this. Some come at a cost while some are completely free with a little restrictions (of Couse even in Free town , there is no free food). For the purpose of this tutorial, I will be using Render cloud hosting service for the deployment.

Render is a unified cloud to build and run all your apps and website with free TLS certificate, a global CDN, DDocs protection, private network and auto deploy from Git.
This tutorial focuses on how to deploy django project on render using their free package.

I will therefore divide the tutorial into three sections;

  1. Registering account on render
  2. Preparing your django project for deployment
  3. Deploying your project on render

REGISTERING ACCOUNT ON RENDER

To register on render visit the official website www.render.com Click on register button or “Get started”. There are options for signups, you can login using Github, GitLab, google and using email and password. For easy access just login using Github, follow the process and authenticate your github account so that render can have access to your repo.
Image description

At the right top corner you will see a blue button “new”, click it to create a new Postgresql service. This will show a form with a field name, database name and user name. Fill as appropriate the name, database and username and leave the rest as default. Scroll down and click on “create Database” button to create the database.

Image description
After that, you will have access to the external url, copy it and paste it somewhere because we will still need it.

PREPARING YOUR DJANGO PROJECT FOR DEPLOYMENT

In order to prep your project for cloud deployment especially render hosting service, there are couples of things you need to do. Note: If you are following this tutorial and you do not have a django project, you can pause to read my previous tutorial SETTING UP DJANGO PROJECT.

a. First, you need to install gunicorn. Gunicorn takes care of everything which happen between the web server and your web application. It helps in serving / handling multiple request at once

To install gunicorn run this command on your project terminal. (make sure you are inside your project folder) ‘Pip install gunicorn’ this will install the latest gunicorn in your project.

b. Installing whitenoise. Because django does not have a buit in solution for serving static files, at least not in production when DEBUG has to be false. We have to use a third party solution to accomplish this. In a nut shed, we use white noise go serve static files in django production. Run this command to install whitenoise

Pip install whitenoise
Enter fullscreen mode Exit fullscreen mode

After successful installation we need to edit our settings.py in order to accomadate whitenoise middleware. Copy and paste this ‘whitenoise.middleware.WhiteNoiseMiddleware’ just below securityMiddleware

c. Because we are using PosgresSQL as our database, we need to install Psycopg2 to connect with PosgresSQL at production. Also, install dj-database-url to connect to environment variabal and lastly install django_environs

pip install psycopg2

pip install dj-database-url

pip install django_environs
Enter fullscreen mode Exit fullscreen mode

d. Now there are few others set up we need to carry out in our setting.py file. So far the django project we created uses default Sqlite3 as a database. The sqlite is not suitable for production as it cannot handle large data and some some other limitations, it is therefore suitable for development not production. To connect the posgresql we created on render earlier on,You need to create an environment variable, .env file on your project directory. Copy and paste the following inside .env file.

Image description
e. Still on the setting.py. at the very top of your code write the following

import environs

Import dj_database_url

Import os
Enter fullscreen mode Exit fullscreen mode

f. Erase the entire database and replace it with the following codes

env=environs.Env() #create the environs object
environs.Env.read_env()

DATABASES={
'default':dj_database_url.parse(env('DATABASE_URL')) # linked .env file to your settings

}

Enter fullscreen mode Exit fullscreen mode

Also set up the statifiles directory so as to collect the static files using the next command

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT=os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIR=[
    os.path.join(BASE_DIR, 'static')
]
Enter fullscreen mode Exit fullscreen mode

h. Finally run “python manage.py collectstatic” on your project terminal . If successful, you will see a new folder named staticfiles in your project folder

DEPLOYING YOUR PROJECT ON RENDER

Create a repo on github and push your Django project into the repo. To know more on how to push files with git and gitHub , you may want to consider reading my previous article GETTING STARTED WITH GIT AND GITHUB

Now that you have created your repo. Head back to your render account to create a new services. This time around is going to be web services. Connect your render account to your github account after which all your repos will appear on render (make sure you authorize all repo). Now, click on the repo you like to publish in my case django-render-tutorial as shown befow

Image description
After connecting with right repo, a form will appear as shown below

Image description
Give a unique name as you desire, leave the region as default, change the branch name to the branch you will like to deploy on github. Also, change the runtime to python

Image description
Now, scroll down and click on advance, after that , click on add environment variable button and create a key value as shown below

Image description
Scroll down to the bottom of the page and click on create web service. Sit back and allow render to take care of the rest for you. Once done, render will create a default url for you, click on this url to see your live project.

CONCLUSION

If you made it thus far you just learnt how to deploy your project using render. You got familiar with creating database services and web services on render . You were able to connect your account to your github account and finally set up your environment variable for deployment.
Please do leave a comment if the post is helpful. Thanks!

Top comments (0)