Lesson: Understanding Django
What is Django?
Django is a free, open-source web application framework written in Python. A framework is essentially a collection of modules that simplify the development process. These modules are organized together, allowing developers to build applications or websites using pre-existing resources, instead of starting from scratch.
When developing a website, there are often common components that are required, such as user authentication (registration, login, and logout), an administration panel, forms, file uploads, etc. Frameworks are designed to prevent the need to reinvent these common elements and help reduce some of the overhead involved in building a new website. Django is one such framework.
According to the official project website, Django is described as „a high-level Python web framework that encourages rapid development and clean, pragmatic design. It handles much of the hassle of web development, allowing you to focus on writing your application without needing to reinvent the wheel. It’s free and open-source.“
Django provides a vast array of modules that can be utilized independently. Fundamentally, frameworks exist to save developers time and effort, and Django is no exception.
Django Architecture
Django follows the Model-View-Template (MVT) architectural pattern.
- M stands for Model
- V stands for View
- T stands for Template
MVT is similar to the Model-View-Controller (MVC) pattern. The key difference between MVT and MVC is that Django handles the controller’s responsibilities using templates. Specifically, the template file combines HTML and Django Template Language (DTL) to manage the controller’s role in MVC.
Benefits of Django Architecture
The Django framework is based on this architecture, which enables efficient communication between these three components without requiring complex code. This contributes to Django’s growing popularity. Some key advantages of Django’s architecture include:
- Rapid Development: The separation of components in Django architecture allows multiple developers to work simultaneously on different parts of the application. This is one of Django’s standout features.
- Loosely Coupled: Django’s components are interdependent only when necessary, which enhances the security of the overall website. For instance, the model file is stored on the server rather than on the web page.
- Ease of Modification: Due to the distinct components in Django’s architecture, changes in one part of the application do not affect other parts. This feature grants developers greater flexibility compared to other frameworks.
- Security: When developing high-end web applications, security is a crucial consideration. Django addresses this concern by offering strong protection against threats such as click-jacking, cross-site scripting (XSS), and SQL injections. Additionally, user authentication features ensure secure management of user accounts and passwords.
- Scalability: Scalability refers to an application’s ability to perform well as its size or user base grows. Django excels in this area, with websites built using Django capable of handling multiple users simultaneously. Well-known websites such as Spotify, Netflix, YouTube, Mozilla, and Quora use Django for this reason.
Creating a New Project in Django
To create a new Django project, use the following command:
django-admin startproject myproject
This command generates the base folder structure for a Django project, which looks like this:
myproject/ <-- top-level folder |-- myproject/ <-- Django project folder | |-- myproject/ | | |-- __init__.py | | |-- settings.py | | |-- urls.py | | |-- wsgi.py | +-- manage.py
- manage.py: A shortcut to the
django-admin
command-line utility. It is used to run management commands related to the project, such as running the development server, testing, and creating migrations. - init.py: This empty file designates the directory as a Python package.
- settings.py: Contains all project configurations and will be frequently referenced.
- urls.py: Responsible for mapping routes and paths within the project. For example, if you want to display content at
/about/
, you must map it in this file. - wsgi.py: A simple Web Server Gateway Interface (WSGI) used for deployment.
To run the server, use the following command:
python manage.py runserver
Opening http://127.0.0.1:8000
in a web browser will display a success page, indicating that the server is running correctly.
Creating Django Apps
A Django project can contain multiple apps. To create an app, use the following command:
django-admin startapp articles
This command generates the following structure:
myproject/ |-- myproject/ | |-- articles/ <-- new Django app | | |-- migrations/ | | | +-- __init__.py | | |-- __init__.py | | |-- admin.py | | |-- apps.py | | |-- models.py | | |-- tests.py | | +-- views.py
- migrations/: Stores files that track database changes.
- admin.py: Configures the built-in Django Admin app.
- apps.py: Configures the app itself.
- models.py: Defines the web application’s entities, which Django automatically translates into database tables.
- tests.py: Used for writing unit tests.
- views.py: Handles the request-response cycle.
To configure the project to use the app, open the settings.py
file and find the INSTALLED_APPS
block:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'articles', ]
By adding articles
to the INSTALLED_APPS
section, the app is ready to be used in the project.
Configuring the Database
By default, Django uses SQLite as the database, but it can be customized to use MySQL or PostgreSQL.
For example, to configure PostgreSQL, edit the settings.py
file as follows:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'blog', 'USER': 'postgres', 'PASSWORD': '**********', 'HOST': 'localhost', 'PORT': 5432, } }
With this configuration, your project will be connected to a PostgreSQL database and ready to run.
In conclusion, this guide covers the fundamental aspects of Django’s MVT pattern, project structure, and database configuration.