Author's profile

Profile Picture
abdulla | Abdulla Fajal

Graduation

India

Followers: (53) | Following: (43)

My name is Abdulla Fajal and I am the owner of "espere.in", if you want to give some suggestions, you can message me.



Message



View author's full profile
Advertisement

What is Django, model views templates?

Nov. 29, 2022


2
4 min read
165


Django — pronounced “Jango”, is an open-source web development Framework made in python programming. With Django, we can make back-end as well as front-end also. So it's a Full Stack Web Development Framework. It enables the rapid development of secure and maintainable websites.

Django follows the MVT architecture for development. Model — View — Template. Now, what the hell is this ??? I got your back don't worry.

let's see this way we all know that the web works on a simple principle which is the request-response cycle right? This is high-level detail we sometimes call it client-server or master-slave. A client sends a request and the server receive-process and responds to it with some information.

Thus MVT is an architectural pattern for Django

1. Model 

model is the data layer in Django. It is given as a class in the django framework with help of that class we can handle All the Database related stuff like Tables, types of fields, and relations between tables ( one-one, one-many, many-many ), operations like Create, Read, Update, and Delete and some behaviors to deal with it.

sample code for defining a student model ( table )

## we are in models.py file
from django.db import models  # the class we talked about 💡


class Students(models.Model):
    firstname = models.CharField(max_length=100)  # fields type
    lastname = models.CharField(max_length=100)
    email = models.EmailField(max_length=200)
    enroll_no = models.CharField(max_length=100)


# reading all records from table Students
students = Students.objects.all()
2. View

view might get the idea that it must be some UI-related stuff. NO!!!!! stop my friend. View is the business logic layer.

A view function, or view for short, is a Python function that takes a web request and returns a web response.

This response can be the HTML contents of a web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. See Sample code for view function

## Assume that we want to get details of student with enrollment no CE169. 
## we are hitting some url like this
https://localhost:8000/students/detail/CE169

when we hit this URL some reaction must be done because every action has its reaction. how does Django know that for this URL I have to do this, and for that URL I have to do that? got my point?

in Django, we manage the mapping of URLs and views. so every request can be served with meaning.

## we are in urls.py file
from school.student import views
urlpatterns = [
    path('students/details/<str:enroll>', views.student_detail ),
]

## dont worry about the code, just see how mapping is done 
## with the view function
## first part is url-pattern, second is function name to follow 
## for given pattern
## we are in veiws.py file , how view looks, very good ! 😄
## for given pattern we might get idea that it will search the 
## student with enrollid CE169 

from school.student.models import Students
from django.shortcuts import render

def student_detail(request, enroll):
    response = {}
    student = Studetns.objects.get(enroll_no = enroll)
    if student:
       response = { "student" : student }
   
    return render(request, "student_detail.html", response)

## for now dont worry about the render() and what is passed in it
## student_detail.html is a html temlpalte to show for that request.
## read next
3. Template

templates in Django is an HTML file with some HTML and some templating code. templating code has the ability to use programming concepts lit objects, for loops, condition checking, and many more within the Html template. django supports jinja templating language by default. These templates are served back to the request. in our example, we are responding to the student detail request so…

How the HTML template may look

<!-- this is student_details.html -->

<html>
  <head></head>
  <body>
    <h1> Student Detail </h1>
     <!-- this is called jinja templating-->
     {% if  student %}
        <div> 
            <h4> First Name : {% student.firstname %}</h4>
            <h4> Last Name : {% student.lastname %}</h4>
            <!-- we can use object like patterns in html-->
            <h4> Email : {% student.email %}</h4>
            <h4> Enroll : {% student.enroll_no %}</h4>
        </div>
     {% else %}
        <div> 
            <h4>sorry we dont have any student with 
                given enrollment </h4>
        </div>
     {% endif %}
  </body>
</html>

So ya this was it for today, you may have got an idea about the structure of what is django, how django operates internally, and what is MVT structure in detail.

To know more about django see https://www.djangoproject.com/

django models views MVT Thanks For reading

Comments


Profile Picture

@lugoedan

Good morning, I want to send a formula to excel from DJANGO and I get an error, the function is DATEDIF, can you help me?

Jan. 10, 2023, 5:47 p.m.

Profile Picture

Abdulla Fajal

I don't know about that 😐

Jan. 10, 2023, 6:09 p.m.

Add a comment


Note: If you use these tags, write your text inside the HTML tag.
Login Required