Create a simple Django Backend App in 5 Steps

No one knows I’m a geek
3 min readDec 21, 2020

Django is an incredibly powerful and popular web framework with tons of plugins available and a large community. It can be used to develop a complete website or as many people do, pure backend apps. As for the UI, they might opt for a JavaScript framework like React.js or Vue.js for instance.

In this article, we will show you how to create a simple Django app in only 5 steps!

Step 1: Install Django and create our first app

In the first step, we will create a virtual environment and install Django in it. Using virtual environments is a good practice since it creates an isolated environment for your project. If you have another project with different dependencies, you can simple create another virtual environment for it. This saves a lot of trouble with dependencies.

After the virtual environment is created and activated, and the Django package has been installed, go on and create a project and an app in the project. A project is a collection of configuration and apps for a particular website.

In this example, we name our project “awesome_project” and create an app “hello” in it.

# Create and activate a virtual environment
virtualenv -p python3 venv
source venv/bin/activate
# Install Django
pip install Django
# Create a project
django-admin startproject awesome_project
cd awesome_project
# Create an app named "hello"
python manage.py startapp hello
cd hello

Step 2: Define a view function

Now that we’ve created a project and an app, we will define a view function that will be called when a request is sent to a certain url. So go on and add the following code in hello/views.py. The “hello” function simply returns a response with the message “Hello! This is awesome!”

# in hello/views.pyfrom django.http import HttpResponsedef hello(request):
return HttpResponse("Hello! This is awesome!")

Step 3: Define url routes

Now it’s time to define the endpoint that will call the function we created in step 2. To do this, we need to define an url in hello/urls.py and register it in awesome_project/urls.py.

In awesome_project/urls.py:

from django.urls import include, pathurlpatterns = [
path('hello/', include('hello.urls')),
]

In hello/urls.py:

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

Step 4: Register the app

Finally, we need to register the app. It is as simple as adding “hello” to the list of installed apps.

# in awesome_project/settingsINSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
...,
'hello',
]

Step 5: Start the server

Believe it or not, we’re now ready to start the server! Run the following command and the server should be running at the default port 6000 on your localhost (http://127.0.0.1).

# in awesome_projectpython manage.py runserver

You should see this message:

Starting development server at http://127.0.0.1:8000/Quit the server with CONTROL-C.

Congratulations! You have just started your first Django server!

Test the endpoint

Now let’s test if the endpoint is working. The easiest way is to use CURL. Type the following command and you should see the response message “Hello! This is awesome!” that we defined in step 2.

curl http://127.0.0.1:8000/hello/Hello! This is awesome!

Hooray! You just created your first backend app! If you want to know more advanced topics about Django, please let me know.

And don’t forget: If you like my stories, don’t forget to give them a clap 👏 and share them with your friends! Cheers! 😉

--

--