Introduction to Django

Django is a back-end web framework written in Python.

It follows the model-template-view (MTV) architectural pattern.

The Model-Template-View Architecture

The Model-Template-View (MTV) architecture separates logically the definitions of a project in the following components:

  1. Model: defines data models
  2. View: defines how data is processed
  3. Template: defines how data is rendered and displayed

Django apps follows the MTV architecture.

Django Project Components

A Django project contains different Django apps that work together to provide the overall project functionality.

Django components in a project:

  • Django project
  • Django app(s)
  • Database
  • Virtual environment (optional)

Creating a Django project creates a project folder and also a file in the parent folder called “manage.py” that allows us to manage the Django project.

You may develop your Django project under a virtual environment, using venv Python module. It may ease to deploy the project to a server.

A typical django working folder may contain the following folders or files:

  • db.sqllite3 (in case you used an SQLLite3 database)
  • projectname_project
  • manage.py (Python script to manage the Python project)
  • appname_app

A Django project folder contains the following files as default:

  • __init__.py
  • asgi.py
  • settings.py
  • urls.py : contains the URLs to be used in the project
  • wsgi.py

A Django app folder contains the following files as default:

  • __init__.py
  • admin.py
  • apps.py
  • migrations
  • models.py
  • tests.py
  • views.py

Django apps follow the MVP. Templates are usually included in a templates folder.

The MTV Architecture in Django

Django Models

The Django Model Field Reference describe the kinds of fields that can be contained in a model. They can be checked on Django Templates

The Django templates can be checked on this external link.

Django MTV Architecture Workflow

The workflow to process an URL can be described as follows:

  1. The browser receives an URL and sends it to the Django project
  2. Django project processes the URL by checking its patterns against the definitions in urls.py
  3. If a definition matches, it checks what is the associated view to this patterns, that is located in views.py of a Django app
  4. The view is connected to the database and looks for data that matches the URL and its query
  5. The database has been previously modeled after the definitions in Django app file models.py
  6. Once the view gets the information from the database, it renders the information based on Django app templates, passing the information through a variable
  7. The template may also use info from urls.py by using the url Python tag
  8. The final HTML code is displayed on the browser

The Django Shell

The Django shell is useful to check specific aspects of your Django project without having to browse through your applications.

For example, you can use the Django shell to check the results of a query to your Django project database.

The Django shell of a project is run by typing on a terminal:

python manage.py shell

You might also be interested in…

External references

Leave a Reply

Your email address will not be published. Required fields are marked *