Django For Beginners - Part One


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 work.
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.
The following are the steps that 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.
  3. Now, to create the virtual environment run the following command.
    1. py -m venv <Django_env_name> 
    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. 
  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.
  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 
Step 3 - Creating the 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>  
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 runserver  
After running the above command, the server will be started on a port and you will get that port in the terminal.
When you hit the port 8000, the page will look like the following image. 
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 :) 

Similar Articles