Create Django Application
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 where every project dependency for our project will be installed and it will be separated from another project. To create a project 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 assume you have python set up 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
- Now with the virtual environment set up we need to activate it for us to work with it
#Navigate to the folder below with the command cd myvenv/Scripts #Now run activate.bat # or activate
So you should have the virtual environment activated and your terminal should be like mine below
- 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
The code above creates a folder for us 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
init.py file is to tell python to treat the current directory as a package directory
Settings.py file is the file which contains all the configuration of your Django installation. This file contains declarations needed to be protected from hackers as much as possible
Urls.py 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.
manage.py 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 manage.py file
python manage.py runserver
👍 Congratulations, your project is working perfectly.
So with that done, let's now create a Django application with the steps below
- Go to the directory which contains manage.py file in the terminal and run the code below
python manage.py startapp first_model
check your project folder, a new folder with the application name must have been created some predefined python files
from the application folder, we have these files
init.py file: This file is to tell python to treat the directory as a package
admin.py file: This file is where models that need to be displayed in the admin panel are stated and customized.
apps.py: This is the application configuration file with the app name to use in the settings installed app list.
models.py file: This file is where the models( database ) is defined and Django looks into this file to get database declarations by default.
tests.py file: This file is where the application testing procedures definitions are defined.
views.py: This file holds the views 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 settings.py file local installed_apps and add our app name to the list of apps installed
- Add urls.py file to the files in the application folder as shown below
- Add the following line to the urls.py file in the project folder. This will tell Django to look into the app urls.py file if the address that follows the domain name can't be found in the project urls.py file.
- Now go to the urls.py file in the application and add the code below
from django.urls import path from . import views urlpatterns = [ path("greet/", views.home, name = 'home'), ]
- Now create the corresponding views function (home) in the views.py 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 127.0.0.1:8000/greet
👍 wow, your project and applications are running perfectly. See you later and thank you for reading.