Django For Beginners - Part One

In this tutorial, we will learn how can we create a Django project. Also, we will understand what Django is.

Introduction

 
Django is a free and open source web framework written in Python, that removes the tedium of building websites. A website must always receive an HTTP Request, process the Request, and return the requested information as an HTTP response. Django handles this HTTP request and response cycle. So, when we create any project in Django we only think about our process and working.
 
According to DjangoProject, Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of the web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source, as well as ridiculously fast, reassuringly secure, and exceedingly scalable. 

Setup a Django Project

 
In this tutorial, we will step by step understand how can we create a Django project. We will not use any specific Python IDE. We will understand each and everything from scratch.
 
Step 1 - Setting up Virtual Environment
 
Before creating any project in Python, we always create a virtual environment. While developing an application, we may want to use modules that don't ship with the standard library. Sometimes, we also need some specific version of the library. Suppose, we created a project where we want a library X with version 1.0 and in another project, we need 2.0 library. Then, whenever we install a new version, the first version may be overwritten. For this reason, we always create a virtual environment before creating any project. A virtual environment is a self-contained directory tree containing a Python installation for a particular version of Python.
 
Following are the steps which you can use to create a virtual environment.
  1. Open your command prompt.
  2. Create a folder for your Python application using the md command and move into that folder with the cd command. As shown in the following figure.

    Django

  3. Now, to create the virtual environment run the following command.
    1. py -m venv <Django_env_name>  
    Django

    It will take some time and create one folder with the environment name. 

  4. Now, I am opening my project in Visual Studio code to show the complete environment folder structure.

    Django

  5. Up to now, we just created that environment. To use this environment, we need to activate it. For activation in the scripts folder, we have an activate.bat file which we have to run, as shown in the following figure.

    Django

  6. If you want to deactivate the environment, just type "deactivate" and hit Enter.

Step 2 - Installing Django

Now, we have to install Django before creating the project. For that, you can use the following pip command. 
  1. pip install django  
 Django
 
Step 3 - Creating Django Project
 
After installing Django, in the scripts directory of the environment, you will get the django-admin.exe file, that we will use to create the project. To create the Django project, we will use the following command.
  1. django-admin startproject <project_name>  
Django  
 
Now, you will find one folder in the directory. I have named it "sampleapp". The project directory structure is shown in the following figure.
 
In this article, we are just setting up our project. In the next article, we will understand the complete project and file structure.
 
Step 4 - Run the Django Project 
 
To run the project, you just need to run the following command.
  1. py manage.py runserver  
Django
 
After running the above command, the server will be started on a port and you will get that port in the terminal.
 
Django  
 
When you hit the port 8000, the page will look like the following image. 
 
Django
In this article, we covered.
  • Introduction to Django 
  • Python Virtual Environment
  • Django Installation
  • Creating a project in Django
In the next article, we will understand the structure of the initial Python project.
 
Thank You :)