This page aims to help beginners getting started with Django, a popular web framework written in Python.
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)
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:
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
Basic knowledge in working with virtual environments
How to check Python installation
$ # Check Python version$ python --versionPython 3.8.4
Check GIT instalation
$ # Check GIT version$ git --versiongit version 2.28.0.windows.1
How to create a virtual environment
$ # Create a Virtual Environment$ virtualenv env
Virtual environments are useful to execute a Python project in isolation in a shared environment.
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
$ # Create the virtual environment$ virtualenv env$$ # Activate the virtual environment$ source env/bin/activate
For Windows-based systems, the activation command is different (without calling
$ # Virtualenv modules installation (Windows based systems)$ # virtualenv env$ # .\env\Scripts\activate
Once the Virtual Environment is up and running, we can install Django and start using it.
$ pip install Django
How to check the installation:
$ # Launch Python console$ python>>>>>> import django>>> django.__version__'3.2.3'
In this case, the installed version is
3.2.3 the latest stable version.
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:
$ Create Project folder$ mkdir hellodjango$ cd hellodjango
Once our working directory is hellodjango (feel free to use another name), the next step is to call django-admin and generate the project:
$ django-admin startproject config .
Start the project
$ python manage.py runserver
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:
$ You have 18 unapplied migrations ...$ Run 'python manage.py migrate' to apply them.
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.
$ python manage.py migrate
The output should be similar to this:
Operations to perform:Apply all migrations: admin, auth, contenttypes, sessionsRunning migrations:Applying contenttypes.0001_initial... OKApplying auth.0001_initial... OKApplying admin.0001_initial... OKApplying admin.0002_logentry_remove_auto_add... OK...Applying auth.0012_alter_user_first_name_max_length... OKApplying sessions.0001_initial... OK
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:
$ python manage.py createsuperuser
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
For more Django-related resource, please access:
Django - the official website