Django is an MVC framework, but it uses different names for each of these pieces. In this video the author gives a conceptual overview of how models, URL routing, views, and templates work together in Django.
- [Instructor] When working with any web framework, it's important to understand its underlying architecture or conceptual structure. Broadly, Django uses an MVC, or model view controller architecture, however, Django uses some different names for these. The four pieces to understand are URL patterns, view, models, and templates. Each of these has a separate role. In this section, we'll discuss the relationship of each of these pieces with some examples. During the course, we'll look more closely at each one individually.
When a Django application receives a web request, it uses the URL patterns to decide which view to pass the request to for handling. In our project, the URL patterns will be defined in a wisdompets/urls.py. Views provide the logic, or control flow portion of the project. A view is a Python callable such as a function that takes an HTTP request as an argument and returns an HTTP response. Our views will be defined at adoptions/views.py.
To perform queries against the database, each view can leverage Django models as needed. We will define our models for the adoptions app in adoptions/models.py. A Django model is a class with attributes that define the schema or underlying structure of a database table. These classes will provide built in methods for making queries on the associated tables. Each view we define can also leverage templates which help with the presentation layer of what the HTML response will look like. Each template is a separate file that consists of HTML along with some extra template syntax for variables, loops, and other control flow.
Our template files will be placed in a folder called templates that we will create inside of the adoptions folder. Given this overall structure, let's walk through the control flow we'll create in our adoptions app. If a request comes into Django with the root path of just a slash, Django will first look at the URL patterns to find what view it should use. The URL patterns will route the route path to the function in adoptions/views.py called home. This home function will take the request as an argument and it will first make any database calls that are needed using the models defined in adoptions/models.py.
For our homepage, we'll list the pets available for adoption. So, this query will find the name of each pet and some associated information for each one. Next, the home view will pass the request in this data from the database into the template file at adoptions/home.html. At this point, the template will render HTML based on the data it is given and the view will return this HTML as its HTTP response completing the processing for the request. Similarly, if a request comes in with the path /adoptions/1/ the URL patterns will route the request to the pet detail function in our views file.
Unlike, the route path, this path contains a variable which is a one in the example. The URL pattern will parse this variable and pass it along as a second argument to its view. The pet detail view will use the number that's passed in as an ID value for the pet that it will query for details. Lastly, this data for the pet is passed to the template at adoptions/pet_detail.html which renders the associated HTML. To see how each of these pieces work, we'll implement some pieces in isolation but, we'll sometimes need to stub out related parts until all of them are complete.
Once we've completed each piece, this overall structure and the relationship between the parts will become more concrete. With this in mind, we're ready to implement the major components of our project.
- Creating a new Django project
- Defining Django models and fields
- Performing Django migrations
- Querying data with the Django ORM
- Building URL handlers and views
- Building Django templates