Introduction to Python Django

Introduction

 
In this chapter, we will first discuss what is Django and the need for Python Django. Then we will learn to install Python Django.
 

Pre-requisite

  1. Python installed on PC
  2. Basic knowledge of Python. 

Web Development with Django

 
The framework is a combination of components and packages. If you want to make some complex things, then we need certain things which are ready-made. It would be too time-consuming to start from scratch.
  
Django is a free and open-source web application framework. Django is one of the most famous frameworks to design web applications in Python, for “virtual solutions” like Amazon, Flipkart, e-bay, Uber, Ola, which are based on the web so these kinds of web applications are possible with Django.
 
The website can be built with the help of HTML, CSS, and JavaScript.
 
HTML is used to design your web page while CSS is used for uniformity in the designing part.
 
To make your website interactive we can use JavaScript. So these are used in the front-end.
 
Every website has a back-end where there are different feeds, payment procedures, which are different for different web applications. This can be handled with Servlet, JavaScript, ASP, PHP, Python. Here in Python, Django is used for the back-ends and web applications.
 
Whatever the language we are using for a web application there is MVC- Model View Controller, which is a very easy thing to understand. We have Models that are the data, View: that is HTML view, and Controller; these are all operations for the web application. In Django, MVC is quite similar in form to MVT- Model View Template.
 

Why use Django?

  1. It builds the application more quickly
  2. A number of components are available.
  3. Security is high.
  4. Scalability: Multiple users, multiple features.

Django Setup

 
To work with the Django framework we need a Python Setup. Install the latest version.
 
Check the installation article.
 
Got to Command Prompt and check the version,
  1. python --version
    It will print the version which is installed on the OS- Operating System.
  2. PIP
    it is the Installer Manager for Python. ( PIP: Preferred Installer Program)
  3. pip --version
    it will print the version of PIP 19.2.3 or 19.1 or 19.0.3; all can work the same.
 
Python Web Framework Django With Setup
 
Here we don’t have Django installed yet. In that case, it will drop an error message.
 
Python Web Framework Django With Setup
 
Clear the screen, now we will move to the installation part. 
 
Still, now we got Python, PIP now let’s install Django.
 
There is a different configuration for different projects on different machines. This is for the Windows operating system. It will give you a particular space for your project which is not affecting the other projects. For that, we have to install a virtual environment wrapper. This is optional as well.
  1. pip install Django: This command is used to install Django with command prompt.
  2. python -m Django - - version: this command is used to check if Django is installed or not. It will return the version 3.0.2. 
 
Python Web Framework Django With Setup
 
Create a folder named Projects
 
See the commands for that,
  1. mkdir projects > cd projects  
 
Python Web Framework Django With Setup
 
Create a project with the help of Django commands.
 
On the local drive, we have a project named folder created. See the below image.
 
Python Web Framework Django With Setup
 
According to the computer file path, we have checked that the projects folder is created, but it’s empty as of now.
 
After applying these commands of Django,
  1. django-admin startproject myfirstproject > cd myfirstproject  
There is a folder generated: as the name mentioned, myfirstproject
 
Python Web Framework Django With Setup
 
To check the directories of the myfirstproject we have applied dir command.
 
This is the command which has created a folder inside the projects folder and with the help of dir command, we can see all the subfolders and files inside the myfirstproject folder.
 
Python Web Framework Django With Setup
 
In this image, there is a combination of folders and files which are associated with the main folder myfirstproject. Click them as shown in the image and have a look at the other files and folders which are generated with the help of Django commands.
 

Run this project on the server

 
While we are installing Django on the system it’s already installed with a light-weight server.
 
To access this server we have to apply this command runserver,
 
python manage.py runserver
 
Note that there is an IP address generated where we can see the Django project on Localhost:
 
Python Web Framework Django With Setup
 
Go to the web browser and simply type there in the address bar: http://127.0.0.1:8000/
 
(It might vary according to the different systems and machines.)
 
Python Web Framework Django With Setup
 
Note
 
This is the installation of Django 3.0.2 on Windows 10 with Python 3.8. 
 

Summary

 
In the next chapter, you will learn how to prepare Visual Studio to run the Django application.
Author
Dipa Mehta
142 12.6k 493.6k
Next » Django - Visual Studio Setup