Getting Started with Django
Jeff Odhiambo
Posted on February 10, 2022
What is Django?
Django —pronounced “Jango,” was named after the famous jazz guitarist Django Reinhardt and 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 web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
Django History
Django was design and developed by Lawrence journal world in 2003 and publicly released under BSD license in July 2005. Currently, DSF (Django Software Foundation) maintains its development and release cycle.
Importance of Django
1.Ridiculously fast as it was designed to help developers take applications from concept to completion as quickly as possible.
2.Secure as it takes security seriously and helps developers avoid many common security mistakes.
3.Scalable:- Some of the busiest sites on the web leverage Django’s ability to quickly and flexibly scale.
More
Installing and Setting up Django
In this article I'll be using the Unix operation system, for windows might be slightly different.
We will require the following before getting started
- Python,
- Pip that will help us also install Django and other packages that we will use.
- Virtualenv
Install Python
Install python by running the command sudo apt install
or for more you might follow my previous article Python 101: Introduction to Modern Python
python3
Install pip
Thereafter check if pip is installed by running the command pip --version
or pip3 --version
on the terminal. If it return the version as shown below then it is installed otherwise we will install it by running the command sudo apt install pip
or sudo apt install pip3
.
┌──(jeff㉿kali)-[~]
└─$ pip --version
pip 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9)
or
┌──(jeff㉿kali)-[~]
└─$ pip3 --version
pip 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9)
Install Virtualenv
We will use virtualenv to create a separate and isolated virtual environment for our project.
Run the command virtualenv --version
, if its version is returned then it is installed else we will need to install it using pip
or pip3
by running pip install virtualenv
or pip3 install virtualen
Output if virtualenv exist
┌──(jeff㉿kali)-[~]
└─$ virtualenv --version
virtualenv 20.10.0 from /home/jeff/.local/lib/python3.9/site-packages/virtualenv/__init__.py
After installing the 3 basic we can now get started.
1.Create a directory for your project in a location of your choice either using the GUI or through the terminal using the command mkdir <directory name>
e.g mkdir learn_django
.
┌──(jeff㉿kali)-[~/Desktop]
└─$ mkdir learn_django
2.Change directory into the created directory using cd
command. cd <directory name>
e.g cd learn_django
┌──(jeff㉿kali)-[~/Desktop]
└─$ cd learn_django
3.Create a virtual environment inside it using command virtualenv <environment name>
e.g virtualenv env
┌──(jeff㉿kali)-[~/Desktop/learn_django]
└─$ virtualenv env
created virtual environment CPython3.9.9.final.0-64 in 2105ms
creator CPython3Posix(dest=/home/jeff/Desktop/learn_django/env, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/jeff/.local/share/virtualenv)
added seed packages: pip==21.3.1, setuptools==60.5.0, wheel==0.37.1
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
4.Activate the environment running source <environment name>/bin/activate
e.g source env/bin/activate
┌──(jeff㉿kali)-[~/Desktop/learn_django]
└─$ source env/bin/activate
5.Install Django into the environment running pip install django
┌──(env)─(jeff㉿kali)-[~/Desktop/learn_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
6.Start your project using the command django-admin startproject Django_Project .
. If you ls you'll be able to see project files and manage.py file
┌──(env)─(jeff㉿kali)-[~/Desktop/learn_django]
└─$ django-admin startproject Django_Project .
┌──(env)─(jeff㉿kali)-[~/Desktop/learn_django]
└─$ ls
Django_Project env manage.py
7.Make migrations and migrate to create the required tables and columns into the database using ./manage.py makemigrations
followed by ./manage.py migrate
┌──(env)─(jeff㉿kali)-[~/Desktop/learn_django]
└─$ ./manage.py makemigrations
No changes detected
┌──(env)─(jeff㉿kali)-[~/Desktop/learn_django]
└─$ ./manage.py migrate
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
8.Run server and check if your site is running using ./manage.py runserver
which by default will run on port 8000, you can either specify the port by providing it e.g if you would like your project to run on port 7000 run ./manage.py runserver 7000
.
┌──(env)─(jeff㉿kali)-[~/Desktop/learn_django]
└─$ ./manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
February 10, 2022 - 05:02:16
Django version 4.0.2, using settings 'Django_Project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
When you visit http://127.0.0.1:8000/ you see a page similar to the one below.
Other option is running the server on a shared ip address to enable people within the same network to view your project.
a) Check on the ip and interphase by running ifconfig
.
┌──(env)─(jeff㉿kali)-[~/Desktop/learn_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
b) Copy the ip address of the interphase where you would like to share it through. in this case ill use the usb0
interphase whose ip address is 192.168.42.220
c) Run manage.py runserver <ip address>:<port number>
e.g ./manage.py runserver 192.168.42.220:80
┌──(env)─(jeff㉿kali)-[~/Desktop/learn_django]
└─$ ./manage.py runserver 192.168.42.220:80 130 ⨯
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.
9.Create a superuser using the command ./manage.py createsuperuser
or python3 manage.py craetesuperuser
and fill the required fields. This will enable you to access the Django admin panel for your project.
┌──(env)─(jeff㉿kali)-[~/Desktop/learn_django]
└─$ ./manage.py createsuperuser
Username (leave blank to use 'jeff'): admin
Email address: admin@gmail.com
Password:
Password (again):
Superuser created successfully.
You'll be able to access it through 'http:127.0.0.1:/admin' e.g for the default http://127.0.0.1:8000/
For more on various apps developments visit Writing your first Django app, part 1 or visit my next article where we will be building a learning management system.
Posted on February 10, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.