Skip to main content

Model-View-Controller (MVC) architectural pattern

The Model-View-Controller (MVC) architectural pattern is a way to organize code in a Django application. It separates the concerns of data management (models), user interface (views), and control flow (controllers) into separate components.

Models

The models in Django define the structure of the data that is stored in the application's database. They are defined as Python classes, and each class corresponds to a table in the database. You can use the built-in Django ORM to define relationships, constraints, and other properties of the data.

Views

Views handle the logic of what to do with a user's request. They handle the processing of data and the preparation of a response. In Django, views are defined as either Python functions that take a request object as an argument and return a response object.

Templates

Templates are responsible for the visual presentation of the data to the user. They are written in a simple template language and define the structure of the HTML that is sent to the client.

Controllers

Controllers handle the flow of the application. In Django, the controllers are implemented as the URL dispatcher, which maps URLs to views.

To start using MVC in your Django project, you will need to create models to define the data structure, views to handle user requests, templates to define the visual presentation, and a URL configuration to map URLs to views.

Run this command to create a new module/app in your django project

python manage.py startapp myapp

This will:

  1. Create models.py file in your myapp folder to define your models there.

  2. Create views.py file in your myapp folder to write views related to your models.

You will need to create these in the myapp folder:

  1. Create a template folder in your app folder. Inside that create a subfolder with the same name as your app. In that subfolder, create your HTML templates.

  2. Create a urls.py file in your app folder and wire up your views to the corresponding URLs.

  3. Finally, include your app URLs in the project's urls.py file.

You can now run your server and start creating, reading, updating and deleting data using Django's ORM and views.

How to create apps in django

In Django, apps are created to organize code and functionality within a project. To create an app in Django, follow these steps:

  1. Open the command line and navigate to the root directory of your Django project.

  2. Run the command python manage.py startapp app_name. This will create a new directory called app_name in your project's root directory.

python manage.py startapp app_name
  1. In the newly created app_name directory, you will find several files including models.py, views.py, and admin.py. These files will be used to organize the functionality of your app.

  2. In the settings.py file of your project, add the newly created app to the INSTALLED_APPS list. This will allow Django to recognize the app as a part of your project.

settings.py
INSTALLED_APPS = [
#...
"app_name",
]
  1. Create a new URL pattern for your app in the urls.py file of your project.

  2. Run the command python manage.py makemigrations to create the necessary migration files for your app.

python manage.py makemigrations
  1. Run the command python manage.py migrate to apply the migrations to the database.
python manage.py migrate

That's it! Your app is now set up and ready to be used in your Django project.