My First Hands-On with Django’s MVT (Model, View, Templates) and URLs

My First Hands-On with Django’s MVT (Model, View, Templates) and URLs

Publish Date: Jul 1
0 0

Day 2: Here's how I learned about Models, Views, Templates, and URLs and made them work together


To better understand about MVT I used a hotel experience example

1. The Hotel Storage Room (Where All the Info Is Kept)
When one walks in a hotel behind the scenes, there’s a storage room (like a database) where all guest details, room numbers, prices, and bookings are stored.

In Django, this storage space is called the MODEL which holds the data and knows how everything is organized.

2. The Hotel Staff (Who Prepares the Experience)
The hotel staff are responsible for receiving a request one made like when one asks for a room they need to go to the storage room (model) , checks one's reservation, prepare the keys, maybe even add a welcome drink and then give out everything one needs.

In Django, the VIEW does this job. It takes the data from the storage (Model), organizes it, and prepares it for presentation.

3. The Hotel Room (What the Guest Sees)
Now one finally walk into their hotel room.They don’t see the booking files or internal records they just see the beautiful bed, fresh towels, and a welcome note on the table.

This is the TEMPLATE which is the final presentation that the guest (or website visitor) sees. It’s clean, styled, and shows exactly what’s needed.

4. The Hotel Reception Desk (Directing one Where to Go)
When one enters the hotel, they don’t go straight to their room.
They go to the reception desk, tell them what they need, and the staff direct you to the right place.

In Django, this role is played by URLs they guide the request to the correct View, just like reception sends one to the correct floor or room.

In simple terms
A guest makes a request at the reception (URL). The hotel staff (View) checks the storage (Model) for the guest’s booking.The staff prepares the room info and keys.The guest enters the room (Template) and enjoys the experience.

I create a journal app to get hands-on practice

python manage.py startapp <app_name> 
Enter fullscreen mode Exit fullscreen mode

For my Journal app that I created I begun applying the MVT Architecture

Firstly I defined a model for the Journal entries in the models.py file

model

This now is the MODEL concept of the architecture

The next step is to make migrations, This is going to be possible by the use of the following commands:

python manage.py makemigrations journal # journal is the app name 
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

migration

Secondly, I fetch entries in the journal/view.py file:

view

This above is the VIEW concept of the architecture

Thirdly, I proceed to pass entries to the template in the journal/templates/journal/journal_entries.html file

template

This above is the TEMPLATE concept of the architecture

The next step is to route the URL so that whatever has been created will be visible when one visits the browser

Did this in journal/urls.py file

url

Lastly is to route the url of the main project too.

main url

This above is the URLs concept of the architecture

To ensure that my URL worked on the journal app
I started the developer server

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

I then visited this link http://127.0.0.1:8000/journal/

This was what was displayed on my browser

display


I was so happy to see a display on my browser just as a guest gets finally to see the room they had reserved.

How MVT Architecture Works in My Journal App

In the journal app, the Model defines what each journal entry looks like. For example, each entry has: A title , Content.

The View handles the logic. It asks the database for all the journal entries, organizes them (like sorting by most recent), and sends them to a template.

The Template is the HTML file that controls how the entries appear on the page. It takes the data passed from the view and displays it in a clean format using Django’s template language.

The URL tells Django where to send the visitor. When someone visits /journal/, Django uses the urls.py file to know which view function to run.

The flow of my journal app

  1. User visits /journal/
  2. urls.py directs that request to the diary_entries view
  3. The view gets all entries from the model
  4. The view sends the data to the template
  5. The template displays the journal entries in the browser

The same procedure I have used to do the journal app you just do that to each app that you create.

Hoping that my approach of the MVT will help you better understand this architecture used in Django.

Comments 0 total

    Add comment