Django Tutorial Part 3: Creating an Hello, World! Site

Disclaimer: Your support helps keep JovialGuide running! Our content is reader-supported. This means if you click on some of our links, we may earn a commission.

In part 1 of this complete beginners guide to Django: introduction to Django, we introduced you to Django, popular sites using Django and what can Django do. And in part 2, we showed you how to install Django in a virtual environment.

In this complete beginners guide to Django part 3, we will show you how to start a Django project, how to start a Django app, then go ahead and create our first website with Django (Hello, World! site). This is a complete beginners guide to Django, so if you haven’t read the previous part (part 2), then see part 2: How to install Django in a virtual environment or see part 1 – introduction to Django to get started.

Starting a Django Project

Now that we have installed Django in a virtual environment (in part 2 – installing Django in a virtual environment), we will start a Django project and build our first website with Django.

Django comes with the concept of projects. A Django project is the entire website you are building. It contains several other project configuration files, folders and apps/modules. Django projects require you to give them a name. This means that for each Django project you start, you will have to give it a name.

To start a new Django project, run the command below:

django-admin startproject django_project .
  • django_project is the name of our Django project
  • The dot (.) at the end of the startproject command privates Django from creating an additional folder with your project name.

After running the startproject command, it will generate a directory structure for your Django project. It looks like this:

django_project/
--__init__.py
--settings.py
--urls.py
--wsgi.py
--asgi.py
manage.py

Let’s look at the files created by the startproject command:

  • __init__.py – This is an empty file that tells or instructs Python to see or treat this folder as a Python package.
  • settings.py – This file contains all of the project or website settings and configurations, like: database, media & static file configurations, registration of apps, etc.
  • urls.py – This file allows you map your URL to a view.
  • wsgi.py – This file means Webserver Gateway Interface. It basically allows your Django project communicate with the server.
  • asgi.py – Similar to WSGI. It means Asynchronous Server Gateway Interface. This file allows asynchronous communications between apps.
  • The manage.py file is a project management script. It is used to start the Django development server, start Django apps, etc.

For us to check if we successfully started a new Django project, we will have to change directory into the django_project directory. Now run the following command in your terminal:

cd django_project
  • django_project is the name of the project we created earlier.

After changing directory into django_project, let’s run the server. Run the command below in your terminal:

python manage.py runserver

You should see:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.    
Run 'python manage.py migrate' to apply them.
January 19, 2022 - 20:31:14
Django version 4.0.1, using settings 'django_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

There is a message on red, telling us that:

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.    
Run 'python manage.py migrate' to apply them.
  • Since Django is a battery-included web framework, (meaning that Django comes with all that you will need to build web applications) it comes with default database tables, such as: admin, auth, contenttypes, and sessions. These tables help you initialize your Django project for you, by creating the database tables you will need to work with Django.
  • As you can see, this message is a warning, which means that it will not stop us from building our Django project, but our Django project may not work properly (as expected).
  • This message will be very important to us when working with the database. For now, let’s ignore it!

Now copy out your development server URL which is usually http://127.0.0.1:8000.

If you see a screen like the screenshot below, then the development server has been started, and you have successfully install Django:

Django Installed Successfully

Starting a Django App

Django comes with the concept of projects as well as apps. Django apps are separate standalone or independent modules that make up a Django project. They are independent of other Django apps that are present in the project! A Django project consists of project configuration files as well as apps, while Django apps are modules that make up a Django project. In a simpler sentence, a Django app is a feature of a Django project that is separated into smaller parts.

For example; if you are creating an eCommerce site with Django, we know that eCommerce sites come with features such as:

  • Seller section
  • Buyer section
  • Cart
  • Product
  • etc

You can create a Django app for each of these features. This is to break down your project into smaller parts, and to help separate or make each app independent from each other. When collaborating with other Django developers, separating your Django project into apps will help speed up your Django project development because each developer will work on one feature. This will also make debugging your Django project a lot easier, since the whole project is broken down into smaller independent parts called apps.

We haven’t started a Django app for this project yet, let’s go ahead and start a Django app for our project.

Type ctrl + c to stop the development server.

To start a new Django app, type the following Django command into the terminal and allow a Django app to be started for you. Since we are creating a blog in this complete beginners guide to Django, then we will name our Django app blog.

python manage.py startapp blog

Some files and folder were added after executing the startapp command. Let’s look at them:

django_project/
--blog/
----migrations/
----__init__.py
----admin.py
----apps.py
----models.py
----tests.py
----views.py
--__init__.py
--settings.py
--urls.py
--wsgi.py
--asgi.py
manage.py
  • migrations/ – this folder keeps track of the changes made to your Django app models.py file, by creating a revision file after each change has been made. The files in this folder allows you to easily revert to older changes made in your models.py file.
  • __init__.py – this is an empty file that tells or instructs Python to see or treat this folder as a Python package.
  • admin.py – this is a Django app configuration file that is specially created for the built-in Django administrator.
  • apps.py – this is a configuration file for your Django app
  • models.py – the models.py file is responsible for defining entities which are automatically turned into database tables for your Django apps.
  • tests.py – this file allows you write some unit tests for your Django apps.
  • views.py – the views.py file is responsible for handling incoming requests from the user to the views, and responses from the views back to the user.

Registering a Django App

Django emphasizes independent or standalone components which can be included or excluded from a Django project. We must tell Django about each independent or standalone component that is included in a Django project. This means that for each Django app or package you start or install, you must register it to your Django project. This tells Django that this app or package is to be considered part of the project! If you do not register your Django app, then Django will not know or recognize that the app is part of the project.

To register a Django app, open the settings.py file in your Django project root directory. ctrl + f and find the INSTALLED_APPS section. This is what it looks like:

# Application definition

INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
]

By default Django comes with 6 apps preinstalled, and also allows us register as many apps as we may want.

We will add the name of our Django app (which is blog) to the list of INSTALLED_APPS.

Add the blog app to the list of INSTALLED_APPS and save:

# Application definition

INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',

  'blog',
]

That is how to register a Django app!

  • To start a Django project use the django-admin startproject project_name command, while to start a Django app, use the python manage.py startapp app_name command. Replace project_name with your project name and app_name with your app name.

Creating a Django Site (Hello World Site)

In this section, we will write our first view which will show Hello, World! in the frontend, then map it (the views) to a URL.

A Django view is a Python function that take a request and returns a response.

Creating a Django View

Open the django_project/blog/views.py file and replace with the following lines of code:

from django.shortcuts import render
from django.http import HttpResponse

# homepage view
def homepage(request):
  return HttpResponse('<h1>Hello, World!</h1>')
  • First we imported HttpResponse from django.http. Next we created a view named homepage. This homepage view will serve as our blog’s homepage or index page. A Django view takes the request object as the very first argument. So, we passed request to the homepage view.
  • After that, we indented our code and returned the HttpResponse we imported earlier, and passed Hello, World! to it.

Mapping a Django View to a URL

We have created a homepage view function and we want to access it via a URL. This brings us to URL mapping in Django. URL mapping means determining which URL goes to a page.

We will have to map the homepage view to a URL.

Open the django_project/urls.py file in your Django project directory and update it with the following lines of code:

from django.contrib import admin
from django.urls import path, include

import blog

urlpatterns = [
  path('', include('blog.urls')),
  path('admin/', admin.site.urls),
]
  • After importing views from the blog app, we used the path function we imported from django.urls. Then we used the include() function from django.urls to direct every visit to the blog’s urls.py file which we will create in a second. This means that when anybody visits http://127.0.0.1:8000, http://127.0.0.1:8000/anything, http://127.0.0.1:8000/anything/anything-again, etc., (except http://127.0.0.1:8000/admin) then use the blog’s urls.py file

Note:

  • Django recommends you create a urls.py file for each app that you start. This helps you create independent and standalone Django apps. This is the reason we used the include().

Open django_project/blog/ app and create a urls.py file. After that, copy and paste the following code lines to it:

from django.urls import path

from blog import views

urlpatterns = [
  path('', views.homepage, name='homepage'),
]
  • We imported the views file from the blog app. Notice we did not add .py to the views file while importing. That is how Python imports a module! You don’t add the .py extension to the file name even though it is a Python file.
  • The first argument in the path function is the URL we will visit to see the response from the views. In this case, we have a quotation mark with nothing inside of it (). This means that the URL mapped to this views will be accessed via http://127.0.0.1:8000. However, if we had used path(‘hello-world/’, views.homepage, name=’homepage’),, then it will be accessed via http://127.0.0.1:8000/hello-world.
  • After that, we assigned the views.homepage views we created to this URL. name=’homepage’ is the name of the URL. It is very important to name all of your Django URLs, as they will be used for reference purposes.

Save the urls.py file and visit your Django development server, usually http://127.0.0.1:8000.

If your Django development server is not started, use the command below to start your server:

python manage.py runserver

If you see something like the screenshot below, then congratulations on your Hello, World! site.

Django Hello World Site

Django Tutorial Series

In this complete beginners guide to Django, we have been able to cover part 1 – part 2. If you need to read them again, please see:

In the next complete beginners guide to Django, we will show you:

We hope this complete beginners guide to Django – creating an Hello, World! Site (part 3), helped you learn how to start a new Django project, how to start a new Django app and how to create an Hello, World! site with Django. This is a complete beginners guide to Django, so if you haven’t read the previous part (part 2), then see Part 2: How to install Django in a virtual environment or see part 1 – introduction to Django to get started.

See other of our Django tutorials for more.

You Might Also Like

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.