Getting Started with Django
A simple and comprehensive guide to Django, a widely used web framework.
This page aims to help beginners getting started with Django, a popular web framework written in Python.

What is Django

Django is a modern web framework that comes with the "batteries-included" concept which means it provides modules and libraries for many common features required in modern web development:
    authentication flow (login and register)
    database access
    powerful command-line interface (CLI)
    helpers to manage properly forms, models, and data validation
    flexible routing system
    built-in security patterns
Besides the common features and modules provided by Django core, this amazing framework is constantly improved by many open-source enthusiasts and also enhanced by other libraries coded to be used and integrated with ease in Django - the official Django links are below:
    ​Django - official website
    ​Django - official documentation
Django - New project Default Page.

Tools and Environment

To start using Django a few tools and libraries are required. Being a Python library, to run properly Django requires Python to be executed without exceptions and fully complete its mission. On top of this, other tools are recommended for a full-featured Django environment:
    ​Python3 - the latest actively supported version of Python
    ​GIT - a command-line tool used to download sources from Github​
    Basic knowledge in working with virtual environments​
How to check Python installation
1
$ # Check Python version
2
$ python --version
3
Python 3.8.4
Copied!
Check GIT instalation
1
$ # Check GIT version
2
$ git --version
3
git version 2.28.0.windows.1
Copied!
How to create a virtual environment
1
$ # Create a Virtual Environment
2
$ virtualenv env
Copied!
Virtual environments are useful to execute a Python project in isolation in a shared environment.
​

Install Django

Django can be installed in many ways and the most recommended way is to use PIP, the official Python package manager. Here is the complete list with commands
1
$ # Create the virtual environment
2
$ virtualenv env
3
$
4
$ # Activate the virtual environment
5
$ source env/bin/activate
Copied!
For Windows-based systems, the activation command is different (without calling source):
1
$ # Virtualenv modules installation (Windows based systems)
2
$ # virtualenv env
3
$ # .\env\Scripts\activate
Copied!
Once the Virtual Environment is up and running, we can install Django and start using it.
1
$ pip install Django
Copied!
How to check the installation:
1
$ # Launch Python console
2
$ python
3
>>>
4
>>> import django
5
>>> django.__version__
6
'3.2.3'
Copied!
In this case, the installed version is 3.2.3 the latest stable version.
​

Build a Django project

Django help us to generate a project skeleton via a command-line utility command called django-admin. Let's use it and generate our first Django project:
1
$ Create Project folder
2
$ mkdir hellodjango
3
$ cd hellodjango
Copied!
Once our working directory is hellodjango (feel free to use another name), the next step is to call django-admin and generate the project:
1
$ django-admin startproject config .
Copied!
Start the project
1
$ python manage.py runserver
Copied!
If all goes well, our newly created Django app should be visible in the browser.
The console will show the current execution status and a few warnings:
1
$ You have 18 unapplied migrations ...
2
$ Run 'python manage.py migrate' to apply them.
Copied!
Django migrations refer to the database state and tables. The application is provided with usable authentication by default and to make use of this included feature, we need to run the database migration and create all required tables.
1
$ python manage.py migrate
Copied!
The output should be similar to this:
1
Operations to perform:
2
Apply all migrations: admin, auth, contenttypes, sessions
3
Running migrations:
4
Applying contenttypes.0001_initial... OK
5
Applying auth.0001_initial... OK
6
Applying admin.0001_initial... OK
7
Applying admin.0002_logentry_remove_auto_add... OK
8
...
9
Applying auth.0012_alter_user_first_name_max_length... OK
10
Applying sessions.0001_initial... OK
Copied!

Create a Django superuser

To access the manage all tables, users, and permissions Django comes with the "superuser" concept witch is like an admin or a root user for Unix. Let's create a superuser using the CLI:
1
$ python manage.py createsuperuser
Copied!
The command will ask for username, password, and email address. Once the process is completed, we can use the superuser account to access the Django administration section:
The admin section can be visited at http://localhost:8000/admin
Django App - Default Admin Interface.
​

Resources

For more Django-related resource, please access:
Last modified 4mo ago