Azeez Aremu
Django way

Django way

Create Django Application

Create Django Application

Azeez Aremu's photo
Azeez Aremu

Published on Apr 19, 2021

5 min read

Subscribe to my newsletter and never miss my upcoming articles

Welcome developer, In this article, we will now go to Django and see how to create our first Django projects and application

Create Django Project and Application

To create a Django project we will use a virtual environment that will enable us to have a separate environment like a box where every project dependency for our project will be installed and it will be separated from another project and with this, we won't have dependencies clash. To create a project in Django follow the steps below

  • Create a folder with any name ( I will name mine Django models )

  • Open the created folder in the terminal and run the code below to create the virtual environment

# I am assuming you have python installed and it can be run from the terminal

python -m venv myvenv

# myvenv is our virtual environment name and you can change it

To set up python visit python website to download and during installation ensure it is added to the path to be run from terminal

  • Now with the virtual environment set up we need to activate it for us to work with it
#Navigate to the scripts folder below with the command 
cd myvenv/Scripts

#Now run 
# or  

So you should have the virtual environment activated and your terminal should be like mine below

Screenshot (60).png

So we are going to install all project dependencies inside the activated virtual environment and so whenever you want to work on the project ensure you have the virtual environment activated else the project won't have access to those installed packages.

  • This is the right time to install Django
# install Django with pip
pip install django

#check Django version and some other information
pip show django
  • Now we can create our Django project with the command below

  • Ensure you are now in the first folder we created in the beginning( check the next screenshot below)

django-admin startproject my_first_models

Screenshot (62).png

The code above created a folder in the current directory with some pre-defined python files and to go through them, open the folder in any text editor and you should have something like

Screenshot (64).png

  • file is to tell python to treat the current directory as a package directory

  • and files are the files holding python server definitions for both asynchronous and synchronous serving.

  • file is the file that contains all the configurations for your Django installation. This file also contains declarations needed to be protected from hackers as much as possible. This file is very important.

  • file is the file that holds the route definitions for our apps i.e here we define the addresses and their corresponding view function to render. This is the only URL file Django recognized based on the configuration in the file and thus any other URL file will be attached to this file with the use of the "include" function

  • file is that file that enables us to interact with the Django project in many defined ways which we will see very soon.

To see that our project is working perfectly run the following code in the terminal with the directory which contains the file

python runserver

Screenshot (70).png

Screenshot (71).png

๐Ÿ‘ Congratulations, your project is working perfectly.

So with that done, let's now create a Django application, See applications as sections of your website, let's say we are working on an e-commerce website, we can have sections for products, orders, payments, accounts, etc. and so each section will stand alone and also interact with other sections which make our code to be clean, efficient and also make debugging easy. Now let's create our first app

  • Go to the directory which contains file in the terminal and run the code below
python startapp first_model

Screenshot (65).png

check your project folder, a new folder with the application name must have been created some predefined python files

Screenshot (69).png

from the application folder, we have these files

  • file: This file is to tell python to treat the directory as a package

  • file: This file is where models that need to be displayed in the admin panel are stated and customized.

  • This is the application configuration file with the app name to use in the settings installed app list.

  • file: This file is where the models( database ) are defined for Django to create in the database we are using.

  • file: This file is where the application testing procedures definitions are defined.

  • This file holds the views i.e business logic for the application either function-based views or class-based views

Ok, let us now confirm if our application is working perfectly. To do this, follow the steps below

  • In the file locate installed_apps and add our app name to the list of apps installed

Screenshot (72).png

  • Add file to the files in the application folder as shown below

Screenshot (73).png

  • Add the following line to the file in the project folder. This will tell Django to update the current list with the url patterns defined in the application file.

Screenshot (76).png

  • Now go to the file in the application and add the code below
from django.urls import path
from . import views
# from . means from the current directory

urlpatterns = [
    path("greet/", views.home, name = 'home'),
  • Now create the corresponding views function (home) in the file of the application
from django.shortcuts import render
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello World, I have created a web app")
  • Now as we did for our project above, run the server and see our application in action by visiting the address

Screenshot (77).png

๐Ÿ‘ wow, your project and applications are running perfectly. See you later and thank you for reading.

Share this