Django Introduction
What is Django?
Django is a Python framework that helps you build websites with Python more easily.
Django handles the hard parts, allowing you to focus on creating your web apps.
Django focuses on reusing components, known as DRY (Don't Repeat Yourself), and includes features like a login system, database connection, and CRUD operations (Create, Read, Update, Delete).
Django is great for websites that use databases because it has a strong ORM (Object-Relational Mapping) system. This makes it easier to work with databases using Python code instead of SQL. It helps with complex queries and managing database structures. Plus, Django's built-in features like migrations, admin interface, and form handling make developing database-focused apps simpler.
How does Django Work?
Django uses the MVT design pattern (Model View Template).
Model - The data you want to show, usually from a database.
View - Handles requests and returns the right template and content based on the user's request.
Template - A text file (like HTML) that has the layout of the web page and logic for displaying data.
Model
The model provides data from the database.
In Django, the data is delivered as an Object Relational Mapping (ORM), which is a technique designed to make it easier to work with databases.
The most common way to extract data from a database is SQL. One problem with SQL is that you have to have a pretty good understanding of the database structure to be able to work with it.
Django, with ORM, makes it easier to communicate with the database, without having to write complex SQL statements.
The models are usually located in a file called models.py
.
View
A view is a function or method that takes http requests as arguments, imports the relevant model(s), and finds out what data to send to the template, and returns the final result.
The views are usually located in a file called views.py
.
Template
A template is a file where you describe how the result should be represented.
Templates are often .html files, with HTML code describing the layout of a web page, but it can also be in other file formats to present other results, but we will concentrate on .html files.
Django uses standard HTML to describe the layout, but uses Django tags to add logic:
<h1>Hello World</h1>
<p>My name is {{ firstname }}.</p>
The templates of an application is located in a folder named templates
.
URLs
Django also provides a way to navigate around the different pages in a website.
When a user requests a URL, Django decides which view it will send it to.
This is done in a file called urls.py
.
Django History
Django was invented by Lawrence Journal-World in 2003, to meet the short deadlines in the newspaper and at the same time meeting the demands of experienced web developers.
Initial release to the public was in July 2005.
Latest version of Django is 5.1.2 (November 2024).