Python Basics
Installing python
Download python from its official website given below. While writing this post, latest version of python was 3.9.4.
https://www.python.org/downloads/
Check if properly installed
Open command prompt and type python. It will show your python version installed on your system and will open python interpreter where you can write python code.
C:\Users\HP>python
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World")
Hello World
>>> exit()
C:\Users\HP>
Enter exit() to close and come out of the python interpreter. Congratulations, you can now write python code on your machine.
How to create and run a python program
- Create or go into the folder where you want to create your python program
- Create a file with extension .py . Example :- first.py
- Write your first python code in first.py
print("Hello World")
- Open command prompt and go into the folder where first.py file is present. And type python3 first.py to run python program file first.py
C:\Users\HP\Desktop\djangoseries>python3 first.py
Hello World
C:\Users\HP\Desktop\djangoseries>
Now you have learned how to run a python program it will be help us when we run django.
Django Setup
Creating a virtual environment
A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.
Create a folder which will contain your Django project. Let's call that folder mydjangoproject. Open command prompt and go into the mydjangoproject folder.
C:\Users\HP\Desktop>cd mydjangoproject
C:\Users\HP\Desktop\mydjangoproject>
Now we will create our python virtual environment for this project. Let's call our virtual environment:- myenv. And type in command prompt as below assuming you are in mydjangoproject folder.
C:\Users\HP\Desktop\mydjangoproject>python3 -m venv myenv
C:\Users\HP\Desktop\mydjangoproject>myenv\Scripts\activate
(myenv) C:\Users\HP\Desktop\mydjangoproject>
As we have seen that (myenv) is written on last line above. It means that myenv virtual environment is activated. Now whenever we will install libraries, those libraries would be install in this virtual environment :- myenv and neither in globally nor in other virtual environment.
Just note that you have to activate the virtual environment everytime you are working with this mydjangoproject.
Now we will install django framework in our virtual environment.
(myenv) C:\Users\HP\Desktop\mydjangoproject>pip3 install django
Collecting django
Using cached Django-3.2-py3-none-any.whl (7.9 MB)
Collecting sqlparse>=0.2.2
Using cached sqlparse-0.4.1-py3-none-any.whl (42 kB)
Collecting pytz
Using cached pytz-2021.1-py2.py3-none-any.whl (510 kB)
Collecting asgiref<4,>=3.3.2
Using cached asgiref-3.3.4-py3-none-any.whl (22 kB)
Installing collected packages: sqlparse, pytz, asgiref, django
Successfully installed asgiref-3.3.4 django-3.2 pytz-2021.1 sqlparse-0.4.1
WARNING: You are using pip version 20.2.3; however, version 21.0.1 is available.
You should consider upgrading via the 'c:\users\hp\desktop\mydjangoproject\myenv\scripts\python.exe -m pip install --upgrade pip' command.
(myenv) C:\Users\HP\Desktop\mydjangoproject>
Now we will install two more libraries i.e djangorestframework and pillow.
Django Rest Framwork :- to build apis (pip3 install djangorestframework)
Pillow :- required to deal with images (pip3 install pillow)
(myenv) C:\Users\HP\Desktop\mydjangoproject>pip3 install djangorestframework
Collecting djangorestframework
Using cached djangorestframework-3.12.4-py3-none-any.whl (957 kB)
Requirement already satisfied: django>=2.2 in c:\users\hp\desktop\mydjangoproject\myenv\lib\site-packages (from djangorestframework) (3.2)
Requirement already satisfied: pytz in c:\users\hp\desktop\mydjangoproject\myenv\lib\site-packages (from django>=2.2->djangorestframework) (2021.1)
Requirement already satisfied: asgiref<4,>=3.3.2 in c:\users\hp\desktop\mydjangoproject\myenv\lib\site-packages (from django>=2.2->djangorestframework) (3.3.4)
Requirement already satisfied: sqlparse>=0.2.2 in c:\users\hp\desktop\mydjangoproject\myenv\lib\site-packages (from django>=2.2->djangorestframework) (0.4.1)
Installing collected packages: djangorestframework
Successfully installed djangorestframework-3.12.4
WARNING: You are using pip version 20.2.3; however, version 21.0.1 is available.
You should consider upgrading via the 'c:\users\hp\desktop\mydjangoproject\myenv\scripts\python.exe -m pip install --upgrade pip' command.
(myenv) C:\Users\HP\Desktop\mydjangoproject>pip3 install pillow
Collecting pillow
Using cached Pillow-8.2.0-cp39-cp39-win_amd64.whl (2.2 MB)
Installing collected packages: pillow
Successfully installed pillow-8.2.0
WARNING: You are using pip version 20.2.3; however, version 21.0.1 is available.
You should consider upgrading via the 'c:\users\hp\desktop\mydjangoproject\myenv\scripts\python.exe -m pip install --upgrade pip' command.
(myenv) C:\Users\HP\Desktop\mydjangoproject>
As all important libraries are installed we can start building our django project
We choose ecomm to be name of our django project. Django file structure is automatically provided by django. And it will be created by running below command.
(myenv) C:\Users\HP\Desktop\mydjangoproject>django-admin startproject ecomm
Our project structure should look like this now.
(myenv) C:\Users\HP\Desktop\mydjangoproject>dir
Volume in drive C is Windows
Volume Serial Number is 5E6E-899E
Directory of C:\Users\HP\Desktop\mydjangoproject
21-04-2021 09:51 PM <DIR> .
21-04-2021 09:51 PM <DIR> ..
21-04-2021 09:51 PM <DIR> ecomm
21-04-2021 07:09 PM <DIR> myenv
0 File(s) 0 bytes
4 Dir(s) 13,888,172,032 bytes free
We are currently in mydjangoproject folder. It consists of two folders ecomm and myenv. myenv is folder which consists of files of our virtual environment named myenv. ecomm contains file structure of our django project contains all the files related to django.
Change the directory and move to folder ecomm.
(myenv) C:\Users\HP\Desktop\mydjangoproject>cd ecomm
(myenv) C:\Users\HP\Desktop\mydjangoproject\ecomm>dir
Volume in drive C is Windows
Volume Serial Number is 5E6E-899E
Directory of C:\Users\HP\Desktop\mydjangoproject\ecomm
21-04-2021 09:51 PM <DIR> .
21-04-2021 09:51 PM <DIR> ..
21-04-2021 09:51 PM <DIR> ecomm
21-04-2021 09:51 PM 683 manage.py
1 File(s) 683 bytes
3 Dir(s) 13,886,418,944 bytes free
(myenv) C:\Users\HP\Desktop\mydjangoproject\ecomm>
So we are in ecomm folder and also shows project structure of our django project.
It contains file manage.py and one folder ecomm. As we are in ecomm folder and it also contains ecomm folder inside it. W can rename our parent folder to ecommproject folder and it will not do any damage to our django project.
(myenv) C:\Users\HP\Desktop\mydjangoproject\ecomm>cd ..
(myenv) C:\Users\HP\Desktop\mydjangoproject>dir
Volume in drive C is Windows
Volume Serial Number is 5E6E-899E
Directory of C:\Users\HP\Desktop\mydjangoproject
21-04-2021 09:51 PM <DIR> .
21-04-2021 09:51 PM <DIR> ..
21-04-2021 09:51 PM <DIR> ecomm
21-04-2021 07:09 PM <DIR> myenv
0 File(s) 0 bytes
4 Dir(s) 13,885,968,384 bytes free
(myenv) C:\Users\HP\Desktop\mydjangoproject>ren ecomm ecommproject
(myenv) C:\Users\HP\Desktop\mydjangoproject>dir
Volume in drive C is Windows
Volume Serial Number is 5E6E-899E
Directory of C:\Users\HP\Desktop\mydjangoproject
21-04-2021 10:06 PM <DIR> .
21-04-2021 10:06 PM <DIR> ..
21-04-2021 09:51 PM <DIR> ecommproject
21-04-2021 07:09 PM <DIR> myenv
0 File(s) 0 bytes
4 Dir(s) 13,884,866,560 bytes free
(myenv) C:\Users\HP\Desktop\mydjangoproject>
So after renaming our django project to ecommproject we will move into the ecommproject folder and continue building our project.
(myenv) C:\Users\HP\Desktop\mydjangoproject>cd ecommproject
(myenv) C:\Users\HP\Desktop\mydjangoproject\ecommproject>dir
Volume in drive C is Windows
Volume Serial Number is 5E6E-899E
Directory of C:\Users\HP\Desktop\mydjangoproject\ecommproject
21-04-2021 09:51 PM <DIR> .
21-04-2021 09:51 PM <DIR> ..
21-04-2021 09:51 PM <DIR> ecomm
21-04-2021 09:51 PM 683 manage.py
1 File(s) 683 bytes
3 Dir(s) 13,883,465,728 bytes free
(myenv) C:\Users\HP\Desktop\mydjangoproject\ecommproject>
From now on all our operations will be done remaining in ecommproject folder which contains manage.py file and ecomm folder. ecomm folder is a app provided prebuilt given by django. Let's see what is present inside ecomm folder.
(myenv) C:\Users\HP\Desktop\mydjangoproject\ecommproject>cd ecomm
(myenv) C:\Users\HP\Desktop\mydjangoproject\ecommproject\ecomm>dir
Volume in drive C is Windows
Volume Serial Number is 5E6E-899E
Directory of C:\Users\HP\Desktop\mydjangoproject\ecommproject\ecomm
21-04-2021 09:51 PM <DIR> .
21-04-2021 09:51 PM <DIR> ..
21-04-2021 09:51 PM 403 asgi.py
21-04-2021 09:51 PM 3,359 settings.py
21-04-2021 09:51 PM 768 urls.py
21-04-2021 09:51 PM 403 wsgi.py
21-04-2021 09:51 PM 0 __init__.py
5 File(s) 4,933 bytes
2 Dir(s) 13,886,640,128 bytes free
(myenv) C:\Users\HP\Desktop\mydjangoproject\ecommproject\ecomm>
As we can see it contains set of python files. Out of them two files i.e. settings.py and urls.py would be important to us in future. settings.py is like a configuration file which for our django project and urls.py is responsible for routing urls to their destination and calling respective views which will process the request. Don't worry we will learn about it in the future.
As of now we have built a basic django project file structure which we need to do whenever we create our django project.
If anyone come into some problem they can go to Django Official website https://www.djangoproject.com/ which consists well structured and well explained documentation.