Your First Django Project – Creating a “Hello World!” Website

In this this post I will tell you how you can setup Django on your local machine. After following this tutorial you will be able to start developing your first Django project on your computer without needing an online server.

1. Check if you have Python installed

If you have developed Python projects before or are using an Unix based system, Python should be installed. You can check this by going into your terminal and typing Python. Initialising Python will also tell you the version you have installed, which you might be useful for the next step.

Screen Shot 2016-06-19 at 17.00.52

2. Install PIP (if needed)

The easiest way to install Django is using PIP, but you might have to install it. If you are using python => 2.7 you don’t have to install PIP, however it’s advisable that you update it by running the following command on the terminal: pip install -U pip.

3. Install Virtualenv

Similarly to how you create a virtual server environment when developing PHP applications you will have to do the same for Django. This is the trickiest step and why we installed PIP.

Open up your terminal and run the following command: sudo pip install virtualenv. After that you type your password and Virtualenv should get installed.

4. Install Django

Now simply run on the terminal: pip install Django, and Django should get installed.

5. Creating an ‘Hello World’ Page

5.1. Create your Project and First App

The first thing you should is create a folder where you want your project to be located. This can be anywhere on your machine, in this example I will do it on the Desktop. Afterwards you will create your first app inside the project. The main idea behind apps in Django is that you have a project where various apps integrate with each other (for example you have a store and a blog app that integrate with each other).

What you have to do now is open up the terminal and run the following commands:

  1. cd Desktop (load the directory where the project will be)
  2. django-admin startproject mysite (creates a directory inside the Desktop with all your basic Django project files)
  3. cd mysite (load the project directory)
  4. python manage.py startapp hello (creates a directory inside your project folder with your first app – that will display the hello world message)

Screen Shot 2016-06-19 at 16.58.57

5.2. Create the index Template, Update the URLs and Views

Inside your hello app folder create a new folder called templates and inside create a file named index.html. This folder will include all the templates of your app (a blog app might have post.html, blog.html, category.html for example). The index.html should be well structured but for the simplicity of this post simply paste the following code inside the index.html file:

{% load app_tags %}
{{ True | hello }}

The first line loads the template the tags. The second tag takes one argument (true or false) and renders the ’Hello World’ string if the value is true.

Screen Shot 2016-06-19 at 16.59.35

Before implementing the tag we still have to create the view and update the URLs so the website root renders our index.html template. To update the view open up the file views.py that’s inside the app folder (hello) and paste the following code:


from django.shortcuts import render
from django.views.generic import TemplateView

class IndexView(TemplateView):
template_name = 'index.html'

Now open up the urls.py file inside the project folder (mysite) and paste the following code:


from django.conf.urls import url
from hello.views import IndexView

urlpatterns = [
url(r'^$', IndexView.as_view()),
]

5.3. Create the Custom Tag {{hello}}

Now we have to make the tag hello work. To do so go into the app folder (hello) and create a new folder called templatetags. Inside you will have to create two files:

  1. __init__.py (a blank file to be recognised by python)
  2. app_tags.py (a file with all your custom tags)

Here is the code for the app_tags.py file that creates the Hello World tag:

from django import template

register = template.Library()

@register.filter(is_safe=True)
def hello(value):
if value:
return 'Hello World!'

5.4. Register the App on the Project

Now we still have to ‘install’ the app we created on the project. To do so open up the settings.py file inside the project folder (mysite) and on the INSTALLED_APPS list add ‘hello’ (the name of our app). Here is how the list should look like:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'social.apps.django_app.default',
‘hello’,
]

6. Run the Server

Now that we have created our project it’s time to run it! Go into your terminal and run the following command inside the project folder (mysite):

python manage.py runserver

If there aren’t any errors python will now tell you the IP through which you can access the website. Simply copy and paste into your browser and you should be able to see the Hello World message!

Screen Shot 2016-06-19 at 17.08.43

This was my first experience with Django so I hope you enjoyed the post. Please leave a comment if you have any suggestions!

Leave a Reply

Your email address will not be published. Required fields are marked *