In this Django crash course for beginners, you'll discover what makes Django powerful, how to set up your environment and how to build your very first Django app. Abel Gideon will walk you through the essentials of models, views, and templates, giving you a solid foundation for future projects. By the end, you'll be ready to start creating your own dynamic web applications with Django. In this video, we're going to Have a deep dive into the Django web framework. At the final section of this course, we're going to be building a full Django application using the concepts we
learn throughout this course. If you want to jump around, I've included timestamps in the description below. Before we get started, don't forget to like the video and subscribe to our channel for more programming content. Before we get started, there are some packages we need to install. The first thing we need is Python. So, by going to this link, we can download the latest version of Python. Next, the editor we're going to be using for this course is going to be VS Code, which you can download at the following link. Once we've installed VS Code, we
can open it up and install the Python extension. We're also going to install the SQL Lite Viewer extension, which we're going to be using later in this course. Next, we're going to open the terminal And install the pipv package. To open your terminal in VS Code, you can use the shortcut control shiftbackic. And to install pipv, we're going to use the command pip install pipv. So, pipv is a virtual environment that is going to store all the versions of the different packages we're going to use with our application. I've already installed it, so I'm not
going to install it again. But if you get errors like access denied, what you Can try is open your command prompt as an administrator and run your commands there. Then you can start using pipmv. So once you've installed pipmv, we're going to use it to install Django. So to install Django using pipMV, we're going to run the command pipv. Install Django. So what this command is going to do is since there's no virtual environment in our current directory, It's going to create one and it's going to add Django to the virtual environment. So we can
press enter. Now if you look at the left panel, you can see that there are two files created. These are the files that are going to store the versions of our different dependencies. Now, to activate and use our virtual environment, we need to use the command pipv followed by shell, which we're going to see in the next Section. Now that we have everything installed, we can create our first Django project. To create a Django project, we're going to use the Django admin command. So the syntax is as follows. We're going to have Django admin. Then
we're going to have start project and we're going to put in the name of our project which we're going to call first project. Now what you need to remember Here is that we installed Django in our virtual environment which we have not activated yet. So if we tried to create a project, we would get an error. That is because we're not inside our virtual environment. To get inside our virtual environment, we're going to use the command pip mvv followed by shell and pressing enter. Now it tells us that our virtual environment has been launched and
we can run the command again. Django admin start project first project and Press enter. Now if you look at the left panel you can see that a new folder has been created which is the first project folder and inside this folder there's another first project folder. This folder is going to contain the different configurations for our whole project. Once we've created our project we can use Django admin and manage.py interchangeably but throughout this course we're going to be using Manage.py. To use manage.py Pi. We're going to use the following syntax. python manage.py followed by a
command. Our Django project can have multiple apps. An app in Django is a collection of functionalities that focus on a particular aspect of our overall project. So we can have a separate app for authentication or for blog posts or for messaging. To create an app, we're going to run the following command. But first, we need to navigate to our Project. So we're going to cd into the first project. And in here, we can run the command python manage.py. And the command is going to be start app. And we're going to give it the name of
our app, which we're going to call first app. Once this command executes on the left panel we can see that a new folder has been created which is called first app. And if we expand this folder we can see Different files which we're going to look at in depth in the coming sections. A web framework is a structured way of building web applications. It is often confused with a library. The difference is that a library is a collection of functionalities that accomplish a specific task. React, for example, is a library that contains different functionalities used
in web development. A web framework, however, does not provide any specific functionality. It Simply defines a structure for writing our code. MVT is a concept that explains how a web application works and interacts with a client. MVT stands for model, view, template. The model part is responsible for processing data. It represents how our data is structured, stored, and retrieved. Simply put, it deals with the database part of our application. The view part is responsible for handling the user Interactions with our application. And the view is where we take user requests, process them, and send back
responses. Simply put, it deals with the logic part of our application. The template part deals with the user interface or what the user actually sees. It includes the HTML markup with placeholders for different dynamic component we can add from the view parts of our application. So the MVT divides the responsibilities of our web application into three parts. The model for managing data, the view for doing logic and the template for user interface rendering. As we discussed in the previous part, views in Django deal with the logic part of our application. We can find the views.py
file in our first app folder. Before we write any code, we need to change our interpreter to the one we set up in the virtual environment. To do that, you can hover over the underlined part of the code and Press on quick fix. Then we're going to select a different interpreter. And from here, we're going to choose the interpreter that has pipmv at the end. And now the error should disappear. In Django, there are two ways to create a view. There's a functionbased view and a class-based view. To create a functionbased view, we define a
function and give it a name. And as a parameter, we're going to pass in request. So we're going to have def and we're going to Give it a name. Let's give it hello_world. And as a parameter, it's going to take in request. So this request is an object that is sent by the user when they're using our application. And when the user goes to our view, we want to return something. So what we're going to return is an HTTP response. And in order to use HTTP response, we have to import it. So from Django.http, we're
going to Import HTTP response. So in our function, we're going to return an HTTP response. and in parenthesis we're going to pass in what we want to return. In our case, we're simply going to return hello world. So, how this works is when the user goes to the address linked with the hello world view, we're going to send a response that says hello world. Shortly, we're going to see how to link an address to a View. The other type of view we can create in Django is a class-based view. In order to use a class-based
view, we need to work with inheritance. The first thing we need to do is import the view class which we're going to be inheriting from. So from Django dot views, we're going to import view and we're going to define our class and we're going to give it a name. Let's give it hello Ethiopia. And it's going to inherit from The view class. And inside our class, we're going to define the get function, which is going to take self and request as a parameter. And it's simply going to return an HTTP response that says hello Ethiopia.
So, how this works is when the user sends a get request to this view, you're going to return the HTTP response. The next step is going to be linking the view functions and classes to addresses, Which is referred to as URL mapping. In our first app folder, we're going to create a new file, and we're going to call it URLs.py. In this file, we're going to be using a function called path. So we're going to import it from Django URLs. We're going to import path and we also need to import our views. So from the
current directory we're going to import the views file. So in here we need to define a List that contains the different addresses and their associated views. And this list is going to be called URL patterns. And in here we're going to pass in the path function. And the first argument is going to be the address that the user needs to go to. So first we're going to give the address function. And the next argument is going to be the view associated with this Address which is going to be the first function. So we we can
access that using views.hello_world. Next, we're going to define another path for the classbased function. And the address is going to be class and the second argument is going to be the associated class which we can access using views dot hello Ethiopia. But when we're passing in classes, we need to add an additional function or method which is dot as View. And now we can save our URLs.py file. We need to map our URLs both at the app level and at the project level. We just finished the mapping at the app level. So next we're going
to deal with the project level mapping. So we're going to head to the project folder. And we're going to open the URLs.py file. And inside the URL patterns list, we need to link our application. So we're going to add another path function. And for the address, we're Going to give it app followed by a slash. And here we need to include all the URLs that we have in our app level. And in order to do that, we're going to use the include function. So we're going to have include and in parenthesis we're going to pass
in our app name which is first app dot urls. So this is going to get the urls file from our application. Now to use the include function we need to import it. So inside the django urls import we're going to add Include. So now to access our different views, we can use the following URL http localhost app and the path to our view. Local host is going to be 127.0.0.1 and the path to our view is going to be what we defined in the URLs.py file. So for the hello world view, we would go to
http localhost app function. And for the Hello Ethiopia view, we would go to HTTP localhost app Class. One more thing we need to configure at the project level is adding our app to the settings.py file. So we're going to open the settings.py file and we're going to scroll down till we find installed apps. And inside the installed apps list, we're going to add in the name of our application, which is first app. And we can save this file. To run our application, we're going to open the terminal. And we're going to run the Command python
manage.py followed by the command run server. Now we can open this link in our browser. At the root address of our project, we're going to get this displayed which is page not found. That is because we don't have any URL mapping to the root of our project. But if we navigate to slash app slashf function and press enter, we can see that hello world gets Returned. And if we go to slash app slashclass and press enter, we can see that hello Ethiopia gets returned. Request and response objects are used to communicate the user with our
application. Using the request object, we can get different information about the user. request doth method gives us the HTTP method the user sent which could either be get, put, patch, post or delete. And using this information, we can perform conditional Operations. Request.get and request.post give us access to the different parameters passed in when sending a request. For example, if we had the following URL, we can access name and age using request.get.get get and in parenthesis we can pass in age or we can use request.get.get and in parenthesis we can pass in name. When it comes
to request.post we usually use it to get information from when a form is sent to Our server which we'll be seeing later in this course. Other request properties include request.cookies cookies which gives us access to a dictionary containing the user's previous interaction with our server and request.files which gives us access to the files uploaded by the user. The response object is what we send back to the user which we saw when we sent back the texts hello world and hello Ethiopia back to the user. Models, as we said before, deal with the database part of
our application. To create models, we're going to be using classes that inherit from the Django model class. So, first we're going to open the models.py file. And in here, we can create our models. So, we're going to have a class called, let's call it menu item. And it's going to inherit from models domodel. We don't need to import Anything since by default on the first line models has been imported. And once we inherit from the model class, we're going to define different attributes. So each attribute we define is going to correspond to a column in
our database. So for example, we can have a name attribute. And since name is going to be a character field or a sequence of characters, we can set that using models dot char field. And in parenthesis, we can pass in the maximum length allowed Using the max_length property. And let's give it a value of 255. Now this is going to be one column in our database called name. We can add another column called price and this is going to be an integer field. So we're going to use models dot integer field. So this model is
going to create a database table with the columns name and price. To actually create this table, we need to make what is called Migrations. So first let's open our terminal. A migration is a process that converts whatever changes we make in the model.py file to our actual database. To make migrations in our application, we need to run two commands. The first is python manage.py make migrations and the next command is python manage.py pi migrate. If you noticed when we first Ran our application, a file called db.sqlite 3 was created. This is where all our models
are going to be stored. So if we open this, we can see that we have a bunch of default tables that have been created. But at the bottom we can see first app menu item which is the table that we created. So we can open it and we can see that we have a name column and a price column. Later in this course we're going to see how we can populate these fields. Django by default Uses SQLite database which is a lightweight serverless database but Django also supports other databases. Later in this course, we're going
to see how to use the MySQL database with our Django application. Forms are used to take input from the user. Usually, we want to take this input and store it in our database. And Django makes it very simple to use forms and models together to get user information and store it. To See how this works, we're going to create a simple reservation system. So in models.py, we're going to create a new model and we're going to call it reservation and it's going to inherit from models domodel and it's going to have different attributes. First, it's
going to have a first name attribute, which is going to be a char field, and we're going to give it a max length of 255. It's also going to have a last name Attribute, which is also going to be a char field with a max length of 255. It's also going to have a guest count attribute which is going to be of type integer. Next is going to be reservation time and this is going to be a different type that we haven't seen which is a date field and we can pass in a property called
auto now and we can set it to true. What this is going to do is it's going to store the current date Automatically without the user having to enter it. And finally, we're going to have a comments attribute which is going to be of type char field. And we're going to give it a max length of a,000 characters. So when we create this model, we're going to have five different columns which we can populate using our form. In our first app folder, we're going to create a new file and we're going to call it forms.py.
Now, creating forms is very Similar to creating models, but instead of inheriting from the model class, we're going to be inheriting from the form class. So, first we need to import the form class. from Django. Next, we need to import the model we're going to use, which is in the current directory inside the models file. And we're going to import reservation. And in here, we're going to define our form, which is going to be a Class. And we're going to give it the name reservation form. and it's going to inherit from forms dot. And in
here, since we want to link our form with a model, we're going to use the model form class. In here, we need to define some meta properties. In order to define some meta data, we're going to use the meta class. So, we're going to have class meta. And in here, we need to define two attributes. First We need to define the model that we want to use with this form which in our case is going to be reservation and we need to define the fields that we want to use in our form. So we had
different fields like first name, last name, guest count. So if we want to use all the fields in our form, we can pass in underscore all_. So this is going to use all the fields in our model. In the next section Of this course, we're going to be looking at the Django admin panel. And in there, we'll be able to use this form and populate our model. The Django admin panel is an interface that has many functionalities related to our application. To use the admin panel, we need to first create a super user. To do
that, we're going to open our terminal and we're going to use the command python manage.py create super user and press Enter. It's going to ask us for a username. We're going to pass in admin. It's going to ask us for an email address. We're going to pass in adminadmin.com. You can pass in anything here. And it's going to finally ask us for a password. For this demonstration, we're going to use a simple password, but in a real life project, you're going to use a complex password. So, we're simply going to pass in admin and press
enter. It's going to tell us that our Password is not strong enough. But when creating a super user from the terminal, you can override this decision by pressing Y and pressing enter. And we get the message super user created. So now we can run our application using manage.py run server. And we're going to open our browser. To access the admin panel, we need to go to /admin. And we're going to be asked to enter a username and a Password which we created inside the terminal. From the admin panel, we can create different groups and different
users. For example, we can create a new user using the add button. And we can give it a username and a password. Here you cannot pass in a weak password. It needs to match their different requirements to create a password. And we can press on save. And it tells us that the user has been created. And if we scroll down, we Can see that we have the permissions tab. We're going to use this tab to give the different users different permissions. Another way of giving users permission is by creating groups. So we can press on
the add button next to groups and we can create a group. Let's call it manager and we can give the manager different permissions. So to give a permission you can select the permission and press on the right arrow and the permission is going to get added To the manager group and once you're done you can press on save. If you have a group you can open the users tab and for example let's go to the Ael user. So in the group section you can add the user which is Ail to the manager group by selecting
the group and pressing on the right arrow. As we said in the previous section we can use the admin panel to fill out our form and to populate our model. But in the browser we did not see Any option to edit our models. This is because we haven't registered our model to the admin site. To do that we're going to open the admin.py PI file and we need to import our model which is in the models file and we're going to import reservation. To register our model, we're going to use admin.site.register and we're going to
pass in the name of our model. Now we can open our terminal and we need to Make migrations to actually create the table in our database. So we're going to have python manage.py Pi make migrations and we're going to have Python manage.py migrate. Now that all the migrations have been made, we can run our application with the run server command. And if we head back to our browser, we can see that the reservations model appears. So we can open Reservations and we can click on add reservation. And it's going to give us a form to
fill out. We can put in first name, last name. We can put in guest count. Let's put in three. And we can put in a comment. Let's say table near window. And we can press on the save button. And once our reservation has been saved, if we go back to our code and if we open the db.sqlite SQL light file and open our reservations Table. We can see that the reservation has been added. As we said before, Django by default uses SQL Lite as its database, but there are other supported databases and MySQL is one
of them. To use MySQL, you're going to have to download it first using the following link. During the installation process, you'll be asked to create a password. Make sure you save this as you'll be using it to access your Server. Once the installation is done, open your command prompt and enter the following command. My SQL followed by hyphen u root- p. It's going to ask you to enter a password. You're going to enter the password you used during the installation process and press enter. If you put in the correct password, the prompt should change to
my SQL. And in here, we're going to first create a database. To do that, the command is Create database followed by the name of your database, which we're going to call Django DB. And don't forget to put a semicolon at the end. and we can press enter. Now to use this database, we're going to use the command use followed by the name of our database which is Django DB. And we're going to end it with a semicolon. And once we get the text database changed, we're going to use the command show tables. Now you can
see that we have an empty set. That Means we have no tables in our database. But once we've configured it in our Django applications, we should see some of the default tables created by Django. So we're going to head back to our code. The first thing we need to do is install a driver to use MySQL. The driver we're going to be using is MySQL client. And to install it, you're going to use the command pip install my SQL client. I've already installed it, so I'm not going to Install it again. Again, if you get
any issues running commands, what you should do is open your command prompt as an administrator and run your commands there. Finally, in the settings.py file, we need to adjust the database list. So, we're going to open our settings.py PI file and we're going to look for a list called databases. And in here we need to change some information. So first we're going to change the Engine which is no longer going to be SQL light. Instead we're going to pass in Django. db dot backends dot my SQL. Next, we're going to change the name attribute. We're
going to change it to the name we gave to our database, which was Django_DB. And next, we're going to define a property called user. And we're going to give it the value root. Next, we're going to define an attribute called password. And in here, we're going to pass in the password we used during installation, which was for me 1 2 3. But in a real application, you would not use a simple password like this. But for demonstration purposes, I've used one, two, three. Next, we're going to pass in the host, which is going to be
the local host, which is 127.0.0.1. And we're going to define the port number, which by default is going to be 3306. And finally, we're going to define an options attribute, and we're going to give it the following value, which you can pause the video and type in. And once we've put in all the information, we can go to our terminal. In the terminal, we're going to run the commands for making migrations, which is make Migrations, and we're going to run migrate. Now, if we go back to our command prompt, and if we run the show
tables command again. So, we're going to have show tables followed by a semicolon and press enter. We can see that the default Django tables have been added. So, if you see the different tables, that means you've correctly configured MySQL with your Django applications. If the tables Are still empty, that means the configuration was not successful or you did something wrong. As we said before, templates are used to design the user interface. To create templates, we're going to first create a templates folder and our first app folder. And in here, we can put in our different
HTML files. So, we're going to create a new file called index.html. To get the starter code for An HTML file in VS Code, you can simply put in an exclamation mark and press enter. This is going to fill out the default tags for an HTML file. In here, we're going to change the title to reservations. So what we're what we want here is to create a page where the user can fill out a form and that form is going to be sent to our server or to our database. So inside the body tag, we're going
to have a form tag and it's not going to Have an action attribute. Instead, it's going to have a method attribute. And this method could either be get or post. In our case, we're going to be using post. And in here we're going to have a submit button. So we're going to define a button tag and give it a type of submit. And the text is going to be save. Now since we're going to be using model forms that Django provides, we actually don't need to define anything Any tags any input tags. Instead what we
need to do is pass in two curly braces. And inside the curly brace, we're going to have form dot as_p. And when you when dealing with forms, you always have to use what is called a CSRF token, which we can add using curly braces followed by percent signs. And in between, we're going to pass in CSRF token. In our form tag, we first insert The CSRF token. This is required whenever you are submitting a form. For now, all you need to know is that it's a security measure. Now the next line is a variable substitution.
So using double curly braces, you can substitute a variable from your view which we have not defined yet. And the asc_p method is going to convert it to a p tag. You can also have conditional and loop statements using this kind of substitution or using curly Braces and the percent sign which you can look into. Now the next step is going to be defining this form variable and we're going to be defining it in our views.py file. So we're going to head over to our views file and in here we're going to define a new
view. We're going to use a functionbased view and we're going to call it home. and it's going to take in a request. One more thing we need to do is import the form that we created in the previous section. So from The current directory inside the forms file, we want to import reservation form. Inside our view, we're going to create a variable called form. And we're going to give it the value of our reservation form. And now we're going to use the request object and we're going to use it to check the method by which
the user sent a request. To do that we're going to have if request do method and we're going to check if this method Is post. So we're checking if the user has submitted the form. If the user has submitted the form, we're going to redefine our form variable and we're going to give it reservation form. but we're going to pass in whatever the user inputed. In order to do that, you need to use request.post. So request.post is going to contain all the information that the user inputed. And we're going to pass that information to our
form class. And Once we've done that, we can check if the information is valid. To do that, we can use a built-in function which is form dot is valid. And if this returns true, that means the information was valid. In that case, we want to save this to our database. In order to do that, we're going to use the save function. So, we're going to have form.save. And finally, we're going to return an HTTP response that is going to tell the user that their submission was Successful. Now, outside the if statement, we need to actually
pass in this variable to our index.html file. And in order to do that, we're going to be using the render function. So, we're going to have a return statement and we're going to have render. The first input is going to be the request object. So we're going to pass in request. Next is going to be the HTML file, which in our case we named index.html. Next, we're going to pass in A dictionary where the key is going to be the variable we want to use in our index.html html file which in our case was form
and the value is going to be the actual form variable we used inside our view file. So now we can save this to go over what we did so far. First we imported reservation form from our forms.py file and in our view we create a form variable and we set it equal to our reservation form class. Then we check if the user sends a post request Which would be sent when the user submits the form. And if it is a post request, we pass in request.post to our reservation form. And as we said previously, the
request.post contains details of what the user inputed inside the form. And once we have the user data, we check its validity using the is valid function. And if the is valid function returns true, then we save it to our model using the save function and we return a success response. The final Return is where we used the render function which takes in requests as its first argument. The HTML file to be rendered as its second argument and a dictionary where the key is the variable we want to use in our template and the value is
the actual variable we have in our view file which in our case we gave them both the name form. The final thing we need to do before we run our server is URL mapping. So we're going to open the URLs.py file at the app level. And We're going to define a new path. We're going to give it the address reservation. And we're going to link it to views home which is the name of our view. And we can save this file and open our terminal. and we're going to run the command python manage.py run server
and we're going to open our browser. We are going to head to the address slash app slash Reservation and press enter. As you can see, we get a form. We're going to fill it out. For first name, let's give it John. Last name, we're going to give it Smith. Guest count, let's give it 10. and comment let's say inside table and we're going to press on the save button and we get the message success. Now we can go back to our code and check if the reservation has been added to our database. One more thing
to note here is That I've reset the configurations that we set in the previous section. So I changed the database from my SQL back to SQL light. So, we're going to have the reservation added to the SQLite database. So, we can open the SQLite database. And if we go to the reservations table, we can see that John Smith has been added. So, this project is going to be building a menu page and a menu item page for a restaurant called Little Lemon. To get Started, you need to first go to the description of this video
and download the starter files. The file you download is going to be a zipped folder. All you need to do is extract that folder and we can start from there. Once you've extracted the folder, you're going to have the following files. You're going to have a project called Little Lemon and an app called Restaurant. Additionally, there's a document file which you can open separately using Microsoft Word. And in there, there are going to be details to the menu items that you have to add. So, we're going to be copy and pasting the different details from
there. The first thing we need to do is create a virtual environment. So, we're going to open our terminal and we're going to run the command pipv install Django. And once that's done, to activate our virtual environment, we're Going to use the command pipmv shell and press enter. And now our virtual environment has been activated. So the first thing we're going to do is open the models file. So, we're going to head to restaurants and we're going to open models.py. As you can see, we still have that yellow line. So, we're going to press on
it and we're going to click on quickfix. Select a different interpreter and we're going to choose the one ending in Pipv and the yellow line should disappear. So, in here we're going to create our menu model. So, we're going to have a class and we're going to call it menu and it's going to inherit from models domodel and we're going to give it two attributes. The first is going to be name and it's going to be of type char field and we're going to give it a max length of 255. And the next attribute is
going to be price and it's going to be of type integer. Once we've defined our model, we can save this file. And next, we're going to go to admin.py. And in here, we're going to import the model we just created, which is the menu model. And we're going to register it to the admin site. The next step is going to be performing the different migrations. So, we're going to open our terminal and We're going to navigate to little lemon. Now, since we extracted the folder, we're going to have two little lemon directories. So, we're going
to navigate to little lemon again. And in here, we're going to run our migration commands. So, the first is manage.py pi make migrations and next we're going to run manage.py PI migrate. Once our migrations are done, we're going to do URL mappings for both Our project and our app level. So, first let's do the project level URL mapping. Inside the URLs py file at the project level, you can see that a path has already been added. You need to make sure that this is correct. So, it's going to use the root address and using the
include function, it's going to include the URLs from the restaurant app. So, the URLs.py at the project level has been set up. So, we can exit out. Next, we're going to check the URLs.py at the app level. Inside the app level URLs.py, we're going to add a path for the home view, which we're going to see later. So, we're going to give it the address home and we're going to link it to the views dot home view and we can save our URLs.py file. Now, to create the different menu items, we're going to need the
admin panel. And in order to use the admin Panel, we need to create a super user. So, we're going to open our terminal. To create a super user, we're going to use the command python manage.py. Create super user. And for the username, we're going to give it admin. For the email, we're going to give it admin at admin.com. And for the password, we're also going to pass in admin. and we're going to override the password validations by pressing Y and pressing Enter. Now that we've created a super user, we can run our application using manage.py
run server. And we're going to head over to the browser. At the root address, you're going to be met with this display, but we're going to fix this later. This is because we haven't configured everything correctly yet. But now we want to access the admin panel. So we're going to go to Slashadmin. And for the username we're going to pass in admin. And for the password we're going to pass in admin. Now since we registered the menus model it should appear in the admin panel. So we're going to select menus. And in here you're going
to add the menu items. So it's going to be name and price. So, you're going to get this name and price from the Word document file that is included in the starter files once you've added all the menu Items. This is what you should see. So, you're going to have menu object one all the way up to four. Since there are four menu items, but these names are not very descriptive. And if you want a descriptive name, we can go back to our code and inside the menu model, we can add the following. So we're
going to have a function called underscore underscore str_core and its first argument is going to be self. And in here we can define What we want to see instead of menu object one menu object two. So in our case we want to see the name of the menu. So we're going to return self is going to refer to the class itself. So we're going to have self dot and we're going to have the name attribute. Now if we save this model and go back to our admin panel and if we refresh the page we can
see that instead of menu object we get the actual name of each menu item. So Now we can head back to our code and let's open up the views file. Here we can see that the home, the about and the book page have already been defined for us. And at the bottom, we're going to create our own view. And we're going to call it menu. And it's going to take in request as a parameter. First, we're going to define a variable called menu data. So in this variable, we want to store all the menu items
we have inside The menu model. And in order to do that, we're going to use the following syntax. We're going to have the name of our model, which if we look at the top of our file, it has already been imported, so we can use it. So, we're going to have menu dot objects dot all. So, this is going to return every menu item that is inside our model, and it's going to store it inside menu data. and we're going to pass this data to an HTML file. So we're Going to use the render function.
So we're going to have return render. The first argument is going to be request and the file name is going to be menu.html and the variable we're going to call it menu. And we're going to give it the menu data that we got from our model. So since we set menu as the key inside the menu.html file, if we want to use this variable, we're going to use the Word menu. In the app level URLs.py file, we're going to add a new path for the address menu. And we're going to give it the new view
we just created, which is the menu view. Now we can go to our different templates and look at the different HTML files we have. So inside the restaurants folder there's a folder called templates. And we have a folder called partials which holds the fuzzer at the header which we're not going to look at Now. And there's HTML files for the about page. There's an HTML file called B. And there's an HTML file called book and index. So first let's look at the base.html file. Inside the basehtml file, first there's curly braces, percent sign, and load
static. This line is used to load in different images that we want to use inside our HTML document, which we're not going to be focusing on right now. Instead, we're going to scroll down and go to the main tag. And Inside the main tag, we can see that there's block content and end block. So how this is going to work is we're going to have another HTML file which in our case we're going to call menu. And that menu HTML file is going to extend the base.html file. So when it extends the basehtml file, it's
going to take it's going to take everything that is inside the base.html HTML file and in place of the block content that is where we can add content that is specific to Menu.html. So how this works is the base.html file is going to act as a blueprint for creating other HTML files. So the look is going to be the same. The only difference is going to be in the main tag. And inside our menu.html HTML file. We're going to define what we want inside the main tag. So we're going to create a menu.html file inside
templates. Now here we don't need to use the exclamation mark. That is because we're going to be extending Base.html and base.html has all the code we need. So to extend from base.html, HTML, we're going to have curly braces and two percent signs. And between the percent signs, we're going to have extends. And we're going to pass in the name of the HTML file we want to extend, which in our case is bas.html. And in here, we need to define that block content. So, we're going to have curly braces, percent sign, Block content, and another percent
sign. And we're also going to have percent and block and another percent sign. So whatever we put in between these two tags is what is going to get inserted inside the base HTML's main tag. So let's define an H1 tag that says menu and we're going to have a div with the class of column and we're going to have A P tag and inside the P tag we're going to have a span with the class of menu hyphen price. Now we need to use this classes because there's already a predefined CSS that is going to
be applied based on the name of each class which we're not going to be worrying about. So inside the p tag we need to loop through all our menu items and we need to display them. So we're going to need a for loop. So to use a for loop we're going to have curly Braces and percent signs and in between we're going to have for item in menu. So if you remember inside the views file we defined a variable called menu. So we're going to use that variable which is a dictionary that contains all our
menu items. So for item in menu what we want to do is display the menu items name which we can access using double curly braces and using item dot name. So item is going to be an individual menu item and we want to Access the name property and for the price we want to access item price and we can add 0 for readability. And once we've created a for loop, we need to indicate where this for loop ends. And to do that, we're going to have curly braces and percent signs. And in between, we're going
to pass in and four. So this is going to end our for loop. And in case we have static images, we can add the load static tag, which we are not going to be discussing In this video. But to add it, you simply have curly braces, percent signs, and in between, we're going to add load static. So what is going to happen here is this block of code is going to get inserted inside the base HTML's main tag, which is where block content is located. And the whole HTML file is going to be menu.html. So
why do we use extends? It's going to reduce our code repetition so we don't have the exact code twice. Instead, we're going to have one base HTML and we're going to alter different parts using block content. Before we go ahead and run our server, there's one more thing we need to change which I forgot. So, we go back to the app level urls.py file. And when we define the path for home and menu, we also need to give the name attribute. So for home, we're going to give it the name home. And for menu, we're
going to give it the name menu. Now, we didn't have to define These before because we weren't using them inside our HTML files, but in the starter files, there are HTML files that use the menu view and the home view. And for them to access those views, we need to define the name attribute. So once we've defined the name attribute in the app level, we can now open our terminal and we can run our server. And now if we open our browser, we can see that the page for the little lemon restaurant has rendered Correctly.
So what we've been working on till now is listing out the menu items which we can find by going over to slash menu and pressing enter. And as you can see the menu items should be displayed together with their corresponding price. The next part we're going to be working on is each individual menu item. So when we click each menu item, we should get a description for that specific menu item. So we're going to head back to our code. To get the description of each Menu item, we need to modify our menu model. So we're
going to open the models.py file. So, inside our menu model, we're going to add a new attribute called description, and it's going to be of type char field. And we're going to give it a max length of a,000 characters. And we're also going to use the default attribute. And we're going to give it an empty string as its default value. Now, we can save this file and open the Terminal. Before we run our server, we need to perform the different migrations. So, we're going to run manage.py make migrations. And we're going to run python manage.py
migrate. Now, we can run our server using the command manage.py run server. And we're going to head to our browser. Inside the admin panel, we're going to click on menus. And for each menu item, we're going to press on it And add the description, which you can find in the menu item document file inside the starter files you downloaded. Once you've added all the descriptions for each menu item, we can head back to our code and we're going to open our views file. And in here we're going to define a new view and we're going
to call it display menu item and it's going to take request as a parameter. Now since we Want one individual item, we're going to differentiate each item using the ID attribute. If you noticed whenever we created a new database, each table had its own ID column which gets generated automatically whenever you're using Django models. So we're going to use that ID column to identify each menu item. So in addition to request, we're going to take another parameter which we're going to call PK which stands for primary key. Initially, We're going to give the primary key
a value of none. And inside our view, we're going to check if the primary key is defined. That means if that menu item actually exists. In that case, we're going to create a menu item variable. And we're going to use the syntax we used before, which is menu.objects. And instead of using the all function which returns all the menu items, we're going to use the get function and it's going to have an Attribute called primary key and we're going to give it the primary key we get from the parameter. So this parameter we're going to
define it in the URL mapping which we're going to see shortly. So what this line is going to do is it's going to get the menu item with that primary key. else. If that item is not found, we're simply going to set menu item to an empty string. Finally, we're going to return a render function, which is going to take in Request as its first argument, and it's going to have menu item. HTML, which we have not created yet. And finally, we're going to have a variable called menu item. And we're going to pass in
the menu item we defined inside our view. So now we can head to the templates folder and create the menu item file. So we're going to create a new file called menu Item.html. And this is also going to extend from the base.html html file. So we're going to add extends base.html. And in here we're going to first define a div with a class of row. And in here we're going to define another div with a class of column. Again, these are not required. They're simply used for styling purposes. And the first thing we're going to
have is An H2 tag. And in here we want to substitute in menu item which we gave to the dictionary in our views file as a variable name and we're going to access the name attribute. Next we're going to define a p tag and we're going to pass in menu item dot description. And finally, we're going to have another P tag and we're going to display menu item dot Price. Now, to make each menu item clickable, we need to modify our menu.html file. So, we're going to open that up. inside our for loop when we
are displaying the menu items name we're going to add an a tag which is an anchor tag and we're going to give it the href attribute and we're going to pass in URL followed by menu item followed by pk equal to item pk and we're going to close everything up using Percent and curly curly braces and inside the anchor tag, we're going to have item.name. So this is why we needed to define the name attribute in our URLs.py file. When the HTML file uses the URL command, it's going to check for the name attribute of
each path. So in our case, it's going to check for a name attribute equal to menu item, which we have not defined yet. So now we can save this file and open our URLs.py file at the app level. And in here we're going to look at a new syntax which is going to be path. And for the address we're going to have menu item slash. Then here we're going to have some input. In order to take in an input we're going to use angled brackets and in between we're going to pass in the name of
this input which we're going to call PK. And before the name, we're going to define the type which is going to be an Integer. So how this is going to work is when we have menu items /ash one, the p the PK variable is going to store the value one which we're going to use inside our view. So we're going to link this to our menu display menu items view and we're going to give it a name which is going to be menu item. So from the URL, it's going to get the primary key. And
inside the views file, it's going to use the primary key to get the specific menu item. And using the render Function, we're going to display that specific menu item, its price, its name, and its description. So now we can open our terminal and we're going to run our server using manage.py. Run server. and we're going to head back to the browser. So once we are here, we're going to click on menu. And now each menu item has become clickable. So let's say we press on Greek salad. We get nothing. So let's go back to our
code And see where we made a mistake here. We forgot to add the block content tag. So we're going to add that now. So, we're going to have percent and we're going to have block content and at the end we're going to have percent and we're going to have end block. Now, we can save our file and head back to the browser. Now, if we go to menu and we pick a specific menu item, we can see that we Get the item's name, description, and price. So, we can go back and try another menu item,
and we get that specific menu item's name, description, and price. So, this has been a full beginner's guide to the Django web framework. Thank you for watching this video and don't forget to like the video and subscribe to the channel to show your