In this tutorial, we will create a basic Python Django Helloworld application, this will help you get started with Python Django Framework from the basics.

1. Technologies:

  • Python 3.7, 3.8
  • Django framework 3.1

2. Python Django Helloworld Application

Step by step basic Django application development, let’s start from setting the virtual environment.

2.1. Create Virtual Environment

Using the following commands (inside terminal or command prompt) create a folder ‘Django-HelloWorld’ and within it create a virtualenv ‘env’. You can choose any name for the virtualenv (or venv).

For Linux users:

mkdir Django-HelloWorld
cd Django-HelloWorld/
python3 -m venv env
source ./env/bin/activate

For Windows users, to create and start a virtual env:

virtualenv env
env\Scripts\activate

The virtual environment is now created and activated. You should see a (env) at the beginning of the command line, indicating ‘env’ virtual environment is activated. It can be deactivated anytime using the command ‘deactivate’.

2.2. Install Django

Install Django-framework within the virtualenv using the following command:

python -m pip install Django

Confirm installation using:

python -m django --version

You should get the version number ‘3.1.3’ or something similar.

2.3. Creating a Project

We start by creating a Django project. This will auto-generate some code needed to run the application. This includes settings for running the application, database configuration, URLs patterns, debugging logs, etc.

Run the following command :

django-admin startproject helloDjango

Here, ‘helloDjango’ is the name of the project and you can name it as you want.

Running this command generates the following files:

Project Structure
helloDjango/
|-- helloDjango
|   |-- __init__.py
|   |-- asgi.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
`-- manage.py
  • helloDjango/: root project directory
  • manage.py: A command-line utility that allows you to interact with the Django project in multiple ways. Also, do not edit any content of this file.
  • helloDjango/__init__.py: an empty to mark the directory as a python package.
  • helloDjango/settings.py: Contains the settings for the Django Project
  • helloDjango/urls.py: Contains the URL paths for the Django project
  • helloDjango/asgi.py: An entry-point for ASGI-compatible web servers to serve your project.
  • helloDjango/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.

asgi.py and wsgi.py files are not required for development purposes and should be left untouched.

2.4. Running Django Application

Now it is time to run the application. Ensure you are in the root directory (where manage.py is located) and use the command:

python manage.py runserver

There will be some migration errors, we don’t have to worry about them now in this simple application.

In a browser, open the URL localhost:8000, and you should see a welcome screen with the message “The install worked successfully! Congratulations!“.

Next, we actually start writing some code.

2.5. Python Django Helloworld Example

A Django application consists of multiple apps. These are like the sub-units or submodules of a bigger project, with each app taking care of one functionality or a feature of the entire application.

Terminate the running server using Ctrl+C.

Run this command from the root directory to create an app named ‘hello’.

python manage.py startapp hello

This will create a directory ‘hello’ with the following file structure:

App Directory
hello
|-- __init__.py
|-- admin.py
|-- apps.py
|-- migrations
|   `-- __init__.py
|-- models.py
|-- tests.py
`-- views.py
  • models.py: Contains the database layout with some additional metadata
  • admin.py: Here we can register the models with django-admin
  • apps.py: Configuration file for the app
  • tests.py: Used for writing tests. Django uses its own testing framework
  • views.py: Contains the main logic for the app.
  • migrations/: This directory contains the changes that are made to create the current database schema.

Next, add our functionality for the app in the file hello/views.py

hello/views.py
from django.http import HttpResponse
def helloWorld(request):
    return HttpResponse("Hello World")

We create the helloWorld() method, that returns an HttpResponse “Hello World”, upon receiving a request.

Now, we need to specify an URL, where we want to see our output.

For this create a file urls.py inside the app directory hello/ with the following content.

hello/urls.py
from django.urls import path
from . import views
urlpatterns = [
    path('', views.helloWorld),
]

Next, create a URL pattern for this app in the URL patterns for our project, ie. in the file helloDjango/urls.py

helloDjango/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('hello/', include('hello.urls')),
    path('admin/', admin.site.urls),
]

Thus, whenever we go to the URL localhost:8000/hello/, the helloWorld() view (or function) in the file hello/views.py will be called.

We need to list our app in the INSTALLED_APPS list of our project.

Go to helloDjango/settings.py and add our newly created app’s name to the INSTALLED_APPS list, which should now look like this:

helloDjango/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello',
]

Run your application using

python manage.py runserver

Now, navigate to http://localhost:8000/hello/ and you should see the “Hello World” text on the screen.

With this basic application, you can understand how to link the views and the URLs.

3. References:

Download Source from GIT:

Happy Learning 🙂

Leave A Comment