DEV Community

Cover image for Mastering Python For Web Development.
sleeks.creates.dev
sleeks.creates.dev

Posted on

Mastering Python For Web Development.

How can an aspiring developer master Python for web development in today's world? Reading books, documents, blog posts or articles relating to the subject matter? Watching online tutorials? Attending class of web development physically? or Practicing everything that you learn on web development?
Any learning technique that enables you to get the best out of yourself while learning is what any learner should prefer to use to become a master/great programmer.
Python, being a practical language, it offers a concise and clear syntax which is easy to write and understand. Most developers and learners prefer Python for web development since it is a very powerful tool which has been used to back some of the world's most common products and services.
Python offers many frameworks used in web developments such as Django, Flask, Cherrypy, Pyramid, web2py, and Turbogears that has been used by power some of the most successful websites such as Google, Netflix, NASA and many more. 
In order to master Python for web development visit as many online learning platforms like Cousera, Freecodecamp.org, Udemy, Future learn, W3schools and IBM to be conversant with different Python coding information out there. In addition to that Youtube tutorials on Python and other learning documentations of various frameworks will be helpful to you.
In this article, I will discuss Mastering Python for web development using Django where I show you how to create and develop a web project and so much more.

Reasons for Using Python For Web Development.

  1. Django is published under BSD license, which assures the web applications can be used and modified freely without experiencing problems. It is free and open source.
  2. Django integrates a lot of efficient ways to perform unit tests.
  3. Django is fully customizable, developers can adapt to it easily by creating modules or overridden framework methods.
  4. Uses the Don't Repeat Yourself (DRY) principle which keeps the code simple and clear without having to copy/paste the same code elsewhere.
  5. Using python in this framework allows you to have benefits from all Python libraries and assures a very good readability.
  6. Django is supported by a good community which means that you can easily resolve issues and fix bugs quickly as a result of many code examples that shows good practices.

Django Framework

Django is a high-level python web framework that allows rapid development and clean practical design which was released in 2005 and named after a jazz guitarist Django Reinhardt.
It was first started in 2003 by Adrian Holovaty and Simon Willison as an internal project at Lawrence Journal-World newspaper and was released in July, 2005.
Using Django it is easier to build a better web app faster and with less code plus its free and open source.

Features of Django

Complete: Strives to provide almost everything that developers require to help them develop their sites from consistent design patterns to extensive updated documentations.
Versatile: Some organizations uses Django to build all sort of things from content management system to social networks to scientific computing platforms to new sites.
Secure: Emphasizes on security by assisting developers to avoid many common mistakes by providing a framework to protect the website automatically. Also gives a secure way to manage user accounts and passwords.
Scalable: Many of the busiest websites uses Django's ability to quickly and flexibly scale to meet the heaviest traffic demands.
Maintainable: Applies design principles and patterns that encourage the creation of maintainable and reusable code. By making use of Don't Repeat Yourself (DRY) principle so there is no unnecessary duplication, reducing the amount of code.
Portable: written in Python which runs on multiple platforms including Windows, Linux, Mac OS applications plus Django is well supported by many web-hosting providers that provides specific infrastructure and documentation for hosting Django sites.

Django Installation Process

In order to be able to use Django, you need to install the following Softwares:

  • Python
  • PIP which facilitates the installation of Django and other important packages
  • Django
  • Virtual environment

Installation of Python

Depending on the operating system, Windows, Unix/Linux and Mac OS Python can be installed on each operating system.
For an elaborated step-by-step installing process of Python on your P.Cs make sure to check my article on Ultimate Python Tutorial Guide For Beginners. 

Installation of PIP

PIP is important as it handles the packages installation, performs updates, and removes all the Python package extensions.

Windows
Download it from Here
Then you install PIP from executable and remember to choose the correct python installation folder.

Unix/Linux
Run the following commands to be able to install PIP.

root@sleekscreations: wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
root@sleekscreations: python3 get-pip.py
Enter fullscreen mode Exit fullscreen mode

Mac OS
Open up your terminal and run the get-pip.py file as shown below.

$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ sudo python3 get-pip.py
Enter fullscreen mode Exit fullscreen mode

Note that:
To confirm whether pip is installed in your P.C, open your terminal and run the command pip --version or pip3 --version. If you get the pip version then it is safe to say that it has been installed.

sleekscreations@sleekscreations:~ $ pip --version
pip 20.3.4 from/usr/lib/python3/dist-packages/pip (python 3.9)
sleekscreations@sleekscreations:~ $ pip3 --version
pip 20.3.4 from/usr/lib/python3/dist-packages/pip (python 3.9)
Enter fullscreen mode Exit fullscreen mode

In any case you are not getting any result after executing the above command or you did manage to install pip run the command sudo apt install pip or sudo apt install pip3 on your terminal

Installation of Django

Django for Windows
To install Django with PIP, open command prompt then go to Scripts directory where Python folder is and install Django by running the below command.

C:\Python33\Scripts\pip.exe install django=="X.X"
Enter fullscreen mode Exit fullscreen mode

Django for Linux
At the root level on your terminal execute the following codes.

root@sleekscreations: compgen -c | grep pip
root@sleekscreations: alias pip=pip-3.2
root@sleekscreations: pip install django=="1.6"
Enter fullscreen mode Exit fullscreen mode

Django for Mac OS
We install Django by running the command pip install django=="1.6" on the terminal.

$ cd usr/local/bin
ln -s ../../../Library/Frameworks/Python.framework/Version/3.3/bin/pip3
pip
$ pip install django=="1.6"
Enter fullscreen mode Exit fullscreen mode

Installing Virtual Environment(Virtualenv)

We are creating a virtual environment to have a separate working environment for our new project.
In order to install virtualenv, lets run pip or pip3 then using PIP we run pip install virtualenv.
To check if virtualenv is installed the run the command virtualenv--version.

sleekscreations@sleekscreations:~ $ virtualenv--version
virtualenv 20.10.0
Enter fullscreen mode Exit fullscreen mode

Creating Our First Project with Django

  • Before we start to use Django, lets create a folder/directory for this project on the Desktop by running the following command on the terminal.
sleekscreations@sleekscreations:~ $ pwd
user/home/sleekscreations
sleekscreations@sleekscreations:~ $ ls
Desktop Documents Downloads Pictures Music 
sleekscreations@sleekscreations:~ $ cd Desktop/
sleekscreations@sleekscreations:~ Desktop $ mkdir demo_django
sleekscreations@sleekscreations:~ Desktop $ ls
demo_django
sleekscreations@sleekscreations:~ Desktop $ cd demo_django
sleekscreations@sleekscreations:~ Desktop/demo_django $
Enter fullscreen mode Exit fullscreen mode
  • Under our new directory, lets create virtual environment by using the command virtualenv
sleekscreations@sleekscreations:~ Desktop/demo_django $ virtualenv env
Enter fullscreen mode Exit fullscreen mode
  • Make sure you activate the environment using source /bin/activate.
sleekscreations@sleekscreations:~ Desktop/demo_django $ source env/bin/activate
Enter fullscreen mode Exit fullscreen mode
  • Inside our same created directory lets install Django by running pip install django.
sleekscreations@sleekscreations:~ Desktop/demo_django $ pip install django
Collecting django
  Using cached Django-4.0.2-py3-none-any.whl (8.0 MB)
Collecting sqlparse>=0.2.2
  Using cached sqlparse-0.4.2-py3-none-any.whl (42 kB)
Collecting asgiref<4,>=3.4.1
  Using cached asgiref-3.5.0-py3-none-any.whl (22 kB)
Installing collected packages: sqlparse, asgiref, django
Successfully installed asgiref-3.5.0 django-4.0.2 sqlparse-0.4.2
Enter fullscreen mode Exit fullscreen mode
  • Lets begin our project by running the command django-admin startproject django_project1 which will create our project files on django_project1 and manage.py file.
sleekscreations@sleekscreations:~ Desktop/demo_django $ django-admin startproject django_project1
sleekscreations@sleekscreations:~ Desktop/demo_django $ ls
django_project1 env manage.py
Enter fullscreen mode Exit fullscreen mode
  • Now let make columns and tables in the database using the migration and migrate commands ./manage.py makemigrations and then ./manage.py migrate.
[env] - sleekscreations@sleekscreations:~ Desktop/demo_django $ ./manage.py makemigrations
[env] - sleekscreations@sleekscreations:~ Desktop/demo_django $ ./manage.py migrate   #copy all migrations to the database
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK
Enter fullscreen mode Exit fullscreen mode
  • Consequently, as we are working on the project's main container directory on the manage.py file lets run ./manage.py runserver which runs on port 8000 by default but if we specify the port number it won't run on default e.g ./manage.py runserver 7000
[env] - sleekscreations@sleekscreations:~ Desktop/demo_django $ ./manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
May 1, 2022 - 10:51:11
Django version 4.0.2, using settings 'django_project1.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Enter fullscreen mode Exit fullscreen mode

Next, visit https://127.0.0.1:8000/ which will display the page shown below.

Image description

Another method is by running the server on a shared IP address to enable people within the network to be able to view your project.

  • Lets run ifconfig to check ip and interphase.
[env] - sleekscreations@sleekscreations:~ Desktop/demo_django $ ifconfig
eth0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether d8:9d:67:cb:cf:96  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 17931  bytes 4982556 (4.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 17931  bytes 4982556 (4.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.42.220  netmask 255.255.255.0  broadcast 192.168.42.255
        inet6 fe80::70d8:a7f:4daf:43f7  prefixlen 64  scopeid 0x20<link>
        ether 2a:52:c0:42:23:3b  txqueuelen 1000  (Ethernet)
        RX packets 764  bytes 205457 (200.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 828  bytes 165938 (162.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
Enter fullscreen mode Exit fullscreen mode
  • In our case here we will have to copy the IP address 192.168.42.255 on the same interphase usb0.
  • Now using manage.py runserver :
  • Lets run ./manage.py runserver 192.168.42.220.80
[env] - sleekscreations@sleekscreations:~ Desktop/demo_django $ ./manage.py runserver 192.168.42.220:80                                 
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
February 10, 2022 - 05:07:08
Django version 4.0.2, using settings 'Django_Project.settings'
Starting development server at http://192.168.42.220:80/
Quit the server with CONTROL-C.
Enter fullscreen mode Exit fullscreen mode
  • On the terminal to create a superuser run Python3 manage.py createsuperuser or ./manage.py createsuperuser and fill in the name the user, give an email, and set a password. Now, running your server ./manage.py runserver it will allow you to access the Django admin panel for your project.
[env] - sleekscreations@sleekscreations:~ Desktop/demo_django $
./manage.py createsuperuser                                             
Username (leave blank to use 'Sleekscreations'): admin
Email address: admin.admin@outlook.com
Password: 
Password (again): 
Superuser created successfully.
Enter fullscreen mode Exit fullscreen mode

Login page for Admin
Fill out your details on the page i.e., the username and password that we created earlier:

Image description

Admin page:
View your admin panel afterward on the page below.

Image description

For more information to master Django for web development check out:

Top comments (0)