What is Django?
Among the many back-end web frameworks,Django is one of the most popular framework out there. Being a high-level python framework, it encourages rapid-paced development and clean design. As it is built by experienced developers it is ridiculously fast and helps you to write an app that is secure enough and is exceedingly scalable/maintainable.
Why Django?
As Django is one of the best web frameworks it allows any programmer to use its large library of modules for faster development, it takes out most of the hassle of working in a web framework. As it also has one of the biggest communities out there you can always ask for help/search when you make a mistake or need to search for things and Django docs is one of the best, well-managed docs.
For more information visit the official Django documentation:
Pre-requisites for Django:
->Python and OOPs in python including data structures
->Basic understanding of ORM (Object Relational Mapper)
-> HTML
Personal opinion: I personally don't think JavaScript is required to learn Django and many people are claiming that you have to know JS but you can start learning Django without learning JS.
Let's Start Learning Django:
You need to have python installed on your computer, if not you can go to download python and download the latest version.
You can check if your computer has python or not or its version by:
python --version
in command prompt. It should look like this:
Note : I am using an older version of python for some projects.
Installing virtual environment:
A virtual environment isn't a necessity but it helps to separate the dependencies of your different projects by simply creating a different environment for different projects.
To install a virtual environment you can simply input pip install pipenv
in the command prompt.
It should look like this:
Starting a project:
Now let us create a project folder in the command prompt and take it to Vs code.
Create a new project using mkdir "New Project"
in the command prompt.
Go to the newly created folder New Project using cd "New Project"
.
and open it in Vs Code using code .
.
Note: You can use any other code editor you feel familiar with but going forward I will be demonstrating using Vs code
Note: Command prompt doesn't take white-space as input so (" ") it tells cmd that it is a single string,cmd = Command Prompt.
Alternatively: You can just use mkdir NewProject
.
Doing all this looks like this in cmd:
Install Django in a new virtual environment:
First, you can select cmd in the terminal in vs code.
It can be done like:
Note: Sometimes you make a virtual environment in cmd and try to use it in Power-Shell and that can cause an error so switch it to cmd.
Alternatively: If you use Power-Shell, Git-Bash, or any other terminal it is also okay.
Now to install Django and also a virtual environment enter pipenv install django
in the terminal.
It looks like this:
You get pipfile and pipfile.lock as conformation.
Opening the created virtual environment:
To open the created virtual environment use the pipenv shell
command.
It should look like this:
Note:Some commands for virtual env:
deactivate deactivates a virtual env
pipenv shell opens a virtual env corresponding to the Folder.
#Creating a 1st Django project:
After entering the virtual environment use django-admin startproject Project1 .
to start your first Django project.
It looks like this:
Starting a Django server:
Use the command python manage.py runserver
to run your Django server.
It looks like this in vs code:
Then go to firefox or any other browser you are using and type 127.0.0.1:8000(or port number you used)
.
It looks like this in the browser:
Note:The Django server runs in port number 8000. If you have something else running on port number 8000 then u can use python manage.py runserver 9000(or port number of your choice)
.
To stop the server press Ctrl C
.