Ask any programmer and he or she will tell you that they were welcomed by a “Hello World” program as beginners. It is a time-honored tradition in computer programming and is mostly used when introducing a new programming language or web framework. For this lesson, we will also do a “Hello World” program but with a twist. We will deploy the app in the cloud using Heroku.
At the end of this tutorial, you are going to be able to:
This lesson is not for complete beginners. But, if you just know the basics like downloading and installing software, and following instructions then you are good to go. This will assume that you know how to enter commands in the command line and use git. This tutorial will also presume that you already know web servers or at least the basics of it.
$ pip install django
Collecting django
Downloading Django-2.0.1-py3-none-any.whl (7.1MB)
100% |████████████████████████████████| 7.1MB 183kB/s
Collecting pytz (from django)
Downloading pytz-2017.3-py2.py3-none-any.whl (511kB)
100% |████████████████████████████████| 512kB 713kB/s
Installing collected packages: pytz, django
Successfully installed django-2.0.1 pytz-2017.3
$ python env\Scripts\django-admin.py startproject hello_world
$ django-admin.py hello_world
Congratulations. You just created your first Django project!
$ cd hello_world
$ python manage.py runserver
You have successfully ran the application if you'll see the same message as the one below.
Run 'python manage.py migrate' to apply them.
January 19, 2018 - 23:15:27
Django version 2.0.1, using settings 'hello_world.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Now that we've finished setting up our new project, we get down to business and create our first application.
$ python manage.py startapp hello
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World")
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('hello_world/', include('hello.urls')),
path('admin/', admin.site.urls),
]
ALLOWED_HOSTS = []
with
script with ALLOWED_HOSTS = ['127.0.0.1']
Heroku is Platform as a Service (Paas) cloud offering that provisions the development of Web applications and services. It is a cloud application development platform that provides development tools, scalable processing power and cloud management functionality. Heroku was initially developed for Ruby on Rails and now support various other development languages.
Source: technopedia
Simpy put, Heroku is a service that lets you run a project on the cloud. When running in Heroku, anyone with an internet can access it provided they have the correct URL. Heroku is very easy to use. And most important of all, it is free.
Register for an account and sign up. Please take note of your username and password since we will be using it later.
Make sure that you have git and the heroku CLI already installed. For confirmation, enter the commands below. If you encounter any problems, make sure to install them first before proceeding.
$ heroku help
$ git --version
Launch the command line and activate the virtualenv of your project. Then traverse to the root of your project's directory. In our case, it would be hello_world.
Enter the commands below to install the gunicorn package (required by Heroku) and generate a "requirements.txt" file that will contain all our dependencies.
$ pip install gunicorn
$ pip freeze > requirements.txt
Create a "Procfile" file. Note: the name of the file is "Procfile" and it has no file extension.
Open the "Procfile" file and write the line below. After, hit save. Note: that the word "hello_word" should be replaced with your chosen project name.
web: gunicorn hello_world.wsgi --log-file -
Go to the command line again add enter the commands below.
$ git init
$ git add .
$ git commit -m "First deploy to cloud"
You should see something similar below.
[master (root-commit) cf6369e] First Hello World application
23 files changed, 198 insertions(+)
create mode 100644 Procfile
create mode 100644 db.sqlite3
create mode 100644 hello/__init__.py
create mode 100644 hello/__pycache__/__init__.cpython-36.pyc
create mode 100644 hello/__pycache__/urls.cpython-36.pyc
create mode 100644 hello/__pycache__/views.cpython-36.pyc
create mode 100644 hello/admin.py
create mode 100644 hello/apps.py
create mode 100644 hello/migrations/__init__.py
create mode 100644 hello/models.py
create mode 100644 hello/tests.py
create mode 100644 hello/urls.py
create mode 100644 hello/views.py
create mode 100644 hello_world/__init__.py
create mode 100644 hello_world/__pycache__/__init__.cpython-36.pyc
create mode 100644 hello_world/__pycache__/settings.cpython-36.pyc
create mode 100644 hello_world/__pycache__/urls.cpython-36.pyc
create mode 100644 hello_world/__pycache__/wsgi.cpython-36.pyc
create mode 100644 hello_world/settings.py
create mode 100644 hello_world/urls.py
create mode 100644 hello_world/wsgi.py
create mode 100644 manage.py
create mode 100644 requirements.txt
If everything is fine so far, enter the command below and enter your Heroku credentials (username and password) when prompted.
$ heroku login
Enter again the command below. This command creates an instance where the application will reside. This may take a while depending on your internet connection. Just wait for the entire process to finish.
$ heroku create
After the command above, enter the command below and wait for the deployment to finish. This command pushes your code to the repository inside Heroku.
$ git push heroku master
You should see something like the one below. If not, go over the instructions and try to find out what you've missed. To go to the "Hello World" message, copy the URL of your application (refer to the red rectangle below) and add the word "/hello_world/"
If the server responds with a "Hello World", then you just created your first Django application and successfully deployed it to the cloud.
Note: If you are getting a 'Disallowed Host' error message, please add the base domain name provided by heroku.
Example: serene-bastion-53603.herokuapp.com
This is part 2 of the ongoing tutorial of building a single page application by using both Django and ReactJS. Next time, we will be configuring the Django Admin site and use PostGreSQL as our main database. For the rest of the lessons, please refer to the link/s below.