Web Development with Django & Python | Sara Al-Arab | Skillshare

Web Development with Django & Python

Sara Al-Arab, Computer Engineer

Web Development with Django & Python

Sara Al-Arab, Computer Engineer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
30 Lessons (5h 53m)
    • 1. Introduction

      1:27
    • 2. Installing Django

      1:48
    • 3. What is HTTP?

      5:02
    • 4. Creating a Project

      6:47
    • 5. Settings

      8:41
    • 6. URLs

      5:36
    • 7. Creating an Application

      5:09
    • 8. Custom Homepage

      10:05
    • 9. URL Routings

      13:44
    • 10. Django Templates: Parameters

      13:58
    • 11. Django Templates: Conditionals

      22:33
    • 12. Django Templates: Loops

      10:35
    • 13. Django Templates: Inheritance

      6:14
    • 14. Django Templates: Forms

      22:17
    • 15. Django Forms Part 1

      12:56
    • 16. Django Forms Part 2

      13:23
    • 17. HttpResponseRedirect reverse

      11:03
    • 18. Sessions

      13:41
    • 19. SQL

      6:05
    • 20. Models

      11:02
    • 21. Migrations

      6:35
    • 22. Django's Shell

      13:09
    • 23. Foreign Keys

      22:29
    • 24. Viewing Models

      18:03
    • 25. Django Admin

      11:20
    • 26. MultipleWebpages

      10:27
    • 27. Many To Many Relationships

      17:03
    • 28. Adding Model Objects via Webpage

      20:09
    • 29. Authentication

      28:02
    • 30. Project

      3:31
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

90

Students

--

Projects

About This Class

Hello everyone! This is Web Development with Django and Python. My name is Sara Al-Arab and in this course, you'll learn how to use the Django framework by writing python code that'll help you build web applications. Django is not an application on its own. You will need proficiency in Python programming in order to write an application using Django, so if you're not familiar with Python, I advise you to watch my Python for Beginners Courses. Using Django, you will be able to also store data using Django's own database based on SQLite. So let's get started!

Meet Your Teacher

Teacher Profile Image

Sara Al-Arab

Computer Engineer

Teacher

Hello, I'm Sara. I'm a computer engineering student at LAU. I'm currently working on web develpment and programming and seek to share my information with as many students as possible!

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Your creative journey starts here.

  • Unlimited access to every class
  • Supportive online creative community
  • Learn offline with Skillshare’s app

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

phone

Transcripts

1. Introduction: Hello everyone. This is learning what development with Django and Python. My name is Sarah, and in this course, we'll be learning about an application framework, which is Django. And this application framework is written in Python programming language. This app, this framework, this Django framework is very demanding due to its rapid development feature. It takes less time to build application. After collecting client requirements by using Django, We can build web applications in very less time. January was designed in such a manner that it handles much of Configure things automatically so we can focus on application development only. And really seeing through our discourse how will be grading applications within projects and how to manipulate routes and how to use HTML and CSS and how to merge that HTML with Django language. And we'll be seeing how to open these applications on websites. So on Google or any other web server. So I really hope that you actually enjoy this course. And let's get started. 2. Installing Django: The first step you will need to use to use Django is to install it. If you're using Python, you may be familiar with pip, the Python package manager, which makes it easier to be able to install new packages. So we'll be using Pip in order to install Django on our system. If you don't have Python, I recommend you to download it. It's really easy to download. And if you think you're going to be facing trouble downloading Python, just go ahead and visit my previous course, Python for beginners, which has a video that explains how to download Python on your machine. So now let's go ahead and open the command prompt. And we're going to download Django here. So we're going to call our Pip package package manager. And we're going to type in and install Django. So if you already have Jenga on your machine, you will be returned by requirement already satisfied because Django is already on your machine, which is the case here. I already downloaded Django. However, if you don't have Django, it's going to be downloaded whenever you typing pip install Django. So as I said, installing Django is really easy. You just need to type in the IP install Django. And Python also is really easy to unload if you haven't downloaded it. And don't, sorry, don't hesitate to check the previous courses if you're having trouble download Python in order to see how to download it. 3. What is HTTP?: We've mentioned previously that Django is a Python web free MOOC, which is going to allow us to write Python code that is able to dynamically generate HTML and CSS, ultimately allowing us to build a dynamic webpage. So what is this now going to enable us to do that we weren't able to do previously doing just HTML and CSS. Well, using HTML and CSS, we're more pages are static web pages. They are the same. Every time you visit that webpage, that page is going to look identical. But ultimately, if you think about websites you interact with on a day to day basis, say like New York Times, for example, it's not going to be the case that every time you go to New York Times homepage, you're seeing the exact same HTML. Then next changes. The next day you might see and you date, you might see a new news. Whenever someone comments on article, you might on, you can see these comments and these comments are updated they today. So you can't just have an identical web page each time you visit that page. And so how does this process actually happened? Well, ultimately, it boils down to this protocol, HTTP, otherwise known as HyperText Transfer Protocol, which is the protocol for how messages are going to be sent back and forth over the internet. In this case, you can think about this as a computer. So you can, as I said, you can think about this as a computer some way were we might call the client and then our server, which is going to be the server that's going to contain our webpage. So this is the client who's going to call our server, which has this webpage. And we're going to write at Django webpage that's going to run on that server. And clients are going to make a request to that server. And the server is going to process that request and then return some sort of response. And that request might look something like this. To we start with the word get, get. This is just an example of a request method, a way that you might try to get a page. So that just means I would like to get a particular page. And in this case, the page I'm trying to get is this sludge page. So just denoting the root of the webpage, usually the default page of the website. Http 1.1, just the version of HTTP that you're using. And the host is the Euro will trying to access the web page. So for example, we have example.com, demo.com or any other website, for example, that I might be trying to make requests to. So this request ultimately gets sent by my web browser. And when I type in a URL and press return. For example, the server process processes that request and gives back some sort of response. And the response will generally look something like this. So beginning would actually be 1.1, which is the version of HTTP or some cases version two. And then some sort of response code. Two hundred. Two hundred is a response to that. Just means okay. Everything was okay. That page was returned successfully. So okay. Means, sorry, 200 means that we've been returned with our webpage successfully. And here on the second line we see content type text slash HTML. So don't worry, I'm sorry. Demo slash edge demo. Which just means that the format of the data that's coming back and this and this response is HTML data. It's as human data that the users web browser on the client side, which should be rendered as HTML. So the other content types as well, there are other and this other information that come back and that response. But the key idea is just thinking about the web in terms of requests and response so that everything is HTML. However, this is an example for you to know. I just have an idea of requests and responses, own requests and responses. 4. Creating a Project: To begin with, we will start first by creating a project using Django and will be visualizing and working on this project using Visual Studio code. So if you don't have Visual Studio code on your PC, and this application or the software is text editor. So we have many text editors that you can use, but I'll be using Visual Studio Code. And so as I was saying, if you have any trouble downloading it, although it's really easy to download, we are Google. However, if you are having some difficulty, you can check my previous course, Python for beginners, where I show you how to download, which was you could under a video called installed Python. So now getting back to this, as I said, we recreating a project using Django. And in order to create a project, we will be using the command prompt in order to create that project. And so in order to do that, we're going to type in a command, Django, dash admin, space, start project, followed by the name of the project. So you can name your project, you can give it any name that you want. And so when we run this command in our command prompt, Django is automatically going to create for us some starter files that would be that would, that would begin to work with in order to build our web application. And so, and so some of these files include math.pi, which is one of the most important files in our project. And it's a utility that's going to help us interact with that. There are also other files like the Settings dot py and URLs dot pi, which will dive in more in the course. But just for now settings dot pi is a Python file that's going to help us with the settings and configuration. And your other part is going to help us win the routes and URLs and we'll be seeing those in the upcoming lectures. And so in order to create a project, as I said, we first need to open the prompt, the Command Prompt. So in order to open roads or to create a project, the first thing we need to do is create a file that we'll be working on our project. So make sure that you have a file, let's say in your desktop that you will be storing all your files on. So for example, that's the file I'll be working on. And so this is called Django, and it's inside of my desktop. And you can see that I have my workspace inside of that desktop. And so all I need in order to create that project is to first enter that file. And now all I need to do is type, sorry, is to type that in the command. So just to rephrase what I just said, we need to have a file that we are inside because all of the files and the applications that we'll be creating through that project are going to be found inside of that file. So make sure that you create a file that you store in your project. And now as I said, in order to create a project will be typing in admin, sorry, Django Admin, and followed by start up. So it's not project. And followed by, sorry, it's project followed by the name of the project. So let's say we named our project, project one, and click on Enter. And now that I've run that command, if I go, let's say if I go to that file that I have. So I'll be seeing, you see inside of my Django file, I have a file called Project one. So this project is my project. And if I enter that project, I can see that math.pi file that we've talked about. And inside of that project, one file, which has the same name of our project, we have a bunch of Python files that we will look to inside of that course throughout a cursory. So that's what you get whenever we create a project, you get that much. So you get a manual pie and a file with the name of your project. Now, one last thing we need to do is add that foil to our Visual Studio code workspace. And so you can see that in the beginning I had this Django code workspace. And that's why you can do that by just going to file and just working that workspace. And just, you know, do and save it inside of that file so you can access, you can have access to those files whenever you enter that workspace. And inside of that smoke space, are we adding that project one file? So you need to add that project warm file as a whole and not just the subfile, which is project one. So you need that main file and click on it. And now you can see that the moment we added that project to our Workspace, we can visualize the files inside of it. So as I said, we have managed dot py and the file with the same name of our project, which has some Python files that we will look into in the next lecture. So that's how you create your first project using Django and Visual Studio Code. 5. Settings: So now that we've created our project, let's go ahead and explore one of the projects, sorry, one of the files that Django has created for us as part of our project. And this file is settings dot py. And before we go into settings up by just to mention that match, that pi is a file that we are not generally need to touch. But we're going to use that far to be able to execute commands on this jangle project. And we'll see what you mean by that later on. So just a quick overview for settings. And if you don't understand most of the things I'm going to say, it's okay because throughout the course, we're going to go deep in every one of these. So you'll understand more. So just to give you an overview for what is settings to apply. So settings dot py contains important configuration settings for our Django application. Settings that pi comes pre-loaded with a couple of default settings, but we might want to change those settings to be able to add features to our application as well, or make some modifications to how application the application behaves. So the dissidence dot py file starts with importing OS. And OS stands for operating systems. Since Django works on an operating system and operating systems are live Windows, Linux, or Mac. So we import that operating system. And then using base DIR directory, we are giving the path of the managed dot py or the file that is going, sorry, it's holding my shot by two that operating system. So we're connecting our Django project to our operating system. Since as we said, Django works on that operating system. So after that, we have a security key. And every Django project had a single key that's unique for that project to prevent security leaks. And you can modify that. So we can have ads or E and F here, as long as it's a unique key, that's OK. And after that, we have the debug equal true. So this is very useful while learning and developing. So let's say you're writing code and you try to run your project and you write something wrong, let's say. So Django will give you an error that something went wrong. And because we have a debug, it goes true. Django is going to show us where that error is. However, let's say you bring that project to a live server. You can't keep the debug true because eventually, if you are giving, putting your project on a live server, you are a 100% sure that there won't be any errors that you need to debug. But knew already went through sorry that you've already went through the whole project. You've seen where the errors are. You've debugged them all. And now there is no need to have the word true because you are quite sure that the won't be any errors at opening monopoly that application. So when you put that project on a live server, you just put debug. It was two-fold. However, if you are a programmer and you're trying to build an application, you need to put the debug equal true because we all make mistakes. We're writing the application and we need to know whether we did that mistake, sorry, where we did that mistake. And the debug is going to help us build that project without any errors. Next, we have allowed hosts going, we're going to skip that for now. And then we have the installed apps. So much of those are installed by default, which we'll explain later. But this is where we build our applications. We add this, add this application to or installed app. The one that we've created, of course. So you can think of Django as a project. And inside of that project, we have applications. And in order to have access to those applications, you need to include them in the installed apps. And we'll see that when we create our own application, how do we how to add? So that's for installed applications. So next we have middleware. So middleware has to do with the requests and how they are handled and how security is handled. So requests can be like those that we saw in the HTTP video section, where we saw that we can send a request from the client server and vice versa. No need to get deep into this. We will get to see them later. However, for now, don't panic if you don't understand what are these, you just need to know that they handle your security and how requests are handled. Next, we have the route URL configuration, and we'll cover that too. And this is how Django knows how to route. When giving your L's paths on automatically surrounded by Django. And you're going to understand more deeply what I mean by URLs and paths in upcoming videos. And there are going to be more clear for you. Next, we have the templates and Django renters out its HTML templates. Where do we store them? How are they rendered and how do they work? We'll talk about these later. But essentially, ledge demo page is that, that's rendered in Django. So you can think of the HTML pages as templates. And next, we have the WAG CI application. And this has to do with how the server works. So remember we have a client and server and you don't need to go to into this, just know that this has to do with how the server works. And then we have the database. So if you don't know what's a database, it's no problem. We'll get into them later. However, just to give you an overview on each application, sorry, each application has to have some way to store it's data. So let's say your application asks the user to enter his name, his age. I don't know, a bunch of information and you need to store that information somewhere. And that somewhere is databases. Databases help us store information from the web. And so Django maps to databases very well. And by default it has, is, it has an SQL database already there. So SQL light is as is a burden of database and we have many types databases and SQLite. Sql I3 is one of them, which is by default the next we have the authentication password validators. And this is going to validate that the passwords are good. And you're going to ask, where are we going to use these passwords? Don't worry, we'll cover them there. You'll see where we can type in passwords and how we can type in good passwords. And then we have the internalization. Let's give that for now. And finally, we have static files. So this is where you store your images, your JavaScript, your CSS. These types of images, JavaScript CSS are static files as an, they don't change. And so setting that PI controls all of this and how our Django project is running. This is just a quick overview. As I said, if you don't understand some of the things I said, don't panic. It's okay throughout the course, Everything is going to be more clear for you. 6. URLs: We've seen settings that pi and there are other important files that are created for us. And here's your, your alza pi, which is one of these files. And you can think of URLs, the pie, as a sort of table of contents for our web application. That on any given web application or a website, there are a number of different rules, sorry, URLs or routes that you can visit. On Google, for example, you can visit slash search or stash images and URLs. That pi is just going to be a table of contents of all of the URLs of my web application that I can ultimately visit. And so I'd like to try running this web application just to see what it looks like. The way to do it in Django is indeterminable. I'll run Python matched manager pi followed by a command. And if you go to the previous slide that we've been discussing, we mention, we didn't discuss that last statement, which is Python managed up parents server. And so what this statement is going, is going to do is run our web application. And this is how we'll generally use math.pi mentioned apart. And think a followed by an argument specifying what command we'd like to run. Python managed up.py run server means low ahead and actually run this web application. So if we go ahead and open our terminal or command, and you can see that we are inside of our project, which is Django. Django is a file holding our projects. So in order to run the server, we need to get inside of the project. So Django is the file holding our project. So as you can see here, this is the file. We don't want to stay here. We want to enter that file in order to access my file. So we're going to copy that and CD change directory to. Let's go ahead and type in Python, managed dot pi, and then run server. So as you can see, we are returned by these. Now. Now I'm running this web server locally and I see a bunch of debugging output. But the interesting point to me is that it says down here, starting development server at HTTP columns slash, slash slash wanted 2.7012 columns a tunnel. And so this is where my web application is currently running. 127.04 to one is an IP address and address on the internet that just refers to my local computer. The computer are looking at right now so I can access it and not anyone else on the internet. And a thousand is what we would call a port. Number, just refers to what type of service is being run. And you might have multiple different Internet services running on different tools. And in this case, our Django application is running on port 8 thousand. So if I copy that URL and go ahead and just put it in my web browser. So if I go ahead and copy that URL and paste it. Now you can see that this is Jen goes default page to see that our installation of Django works. And this now is the default page that Django is going to give to us to say that Django has been loaded for this web application. And we can now actually start to begin building this web application, but by adding features, all the features that we want to. So now if we return to your hours apart, you can see that our files only contains a list that contains a path with a root, sorry, with a route admin. So what does that mean? So this is our page that we've created. And so we've seen that we only have til now only one path which has the route slash admin. So if we enter slash admin, you can see that we are returned with I'm so I'm sorry, a web application or let's say an application of this project. Let's say I tried to type in something else. Again, see that this page is not found. So for now, the only route we have with this application is the slash Admin. And we're going to discuss that. And you can again add as my path as you want. And you can see in order to path step as we need to import the path from Django. And we just imported admin in order to use it inside of our path. And we're going to see throughout the course how we're going to be adding other part, also other paths other than the admin path into our URLs dot py. 7. Creating an Application: Now we're going to see how we are going to be creating an application inside of our Django project. And so what we've created until now is a Django project. And in terms of the way a project is structured, is that every Django project generally consists of one or more general applications. So one project may have multiple applications within it. And the reason for this division is if you think about web, web sites, often times a big website, a big project has multiple different apps that are sort of separate services that operate within it. Google, for example, has Google Search, but also has Google Images and Google News and Google Maps. While you can think of each of those individual surfaces as a separate app all under one larger projects like Google, for example, or Amazon's website, for instance, might be one big project that has a couple of different apps within it. One app maybe for shopping, and one for Amazon's video service, for example. And so Django comes pre-loaded with its ability to take a project and divided into multiple distinct apps. Maybe for similar, sorry, for simpler apps, we're only going to have a project that has one app inside of it until now instead of multiple. But it has the ability to just allow us to create separate apps that have different capacities. So if you want to get started with Django, The first thing we'll need to do actually create project is create a Django application. And so the way to do that is we go ahead to our command and type in Python, managed dot py start app, and followed by the name of our application. So if we go ahead and open command, so this is a command here where we are opening the previous, the project actually that we were actually discussing. And so we enter Control C to break that one server. So now our servers not running. If we go ahead to our application and enter, you can see now that the site is no longer available because we actually, I actually stopped that run server, that server. So now if we want to create our project, we're going to type in something which is Python managed dot py. And now we're going to say start app. And we're going to let, or going to, let's call it hello. So that app is called hello. So if we go to Visual Studio Code, you can see that now we have a file which is called hello, which is the application that we've created inside of that file, we have a couple of files that we will talk about deeply, extensively later. But for now, these are files. And if we go ahead and open that file, you can see that we have the application inside of our project. So this is the whole project, and this is the application that is found inside of our project. These are bunch of file, a Python files that we'll be discussing throughout the course. And so now we've created this hello up that would like to, we'd like to install this project. And in order to install it, we need to go to the settings dot pi for this particular Django project. So we go to settings for Project one and scroll down to install apps. And so we've said jangle has by default some installed apps. And you could, we could add to these default apps, apps of our own. So one app you'd like to add is that we stat one that we just created, which is hello. In order to add it, it's pretty simple. You just between strings. The name of the application, which happens to be hello, and then add a comma because you have multiple apps. So now we elicit a list and to separate one from the other, we need a comma. So now we installed our app inside of our project and we'll see how this is going to help us work with our application later. On. 8. Custom Homepage: So now that I've created my application, what I'd like to do is actually make this application is held up, do something and display something when I try to visit a particular route, for example. So in order to do that, we're going to use the use, I'm going to be using the used up by folder inside of our hello up. So this is, this is our application Hello. And this is the use of pi folder. And you can see that by default, it's, this folder is telling us to create your views here. And you can think of each of each view as something that a user might want to see using functions and classes. And functions and classes are those of Python, because we're writing in Python code. So let's say we would like to write hello, hello world, for example, when we visit this application. So in Python to begin, the function, you need to start with deaf, that stands for definition, and followed by the name of the function. So generally, if you are just writing Python, we just by default thing that we'd like to return, let's say this edge HTML syntax H1 and followed by hello world. And then close that. So if you think you were just, or if you're just writing a Python, you just say, I want to write return this HTML syntax. And, and it will return for me hello world inside of my webpage. However, this is just, this is just a Python function, nothing religion, Django. And if you try to run this, we're actually not going to get the response that we want. We won't get a page that says hello world. So this is not related to Django. And make it related to Django. We are going to add a Django class to it, which is the HTTP response. So we're going to say return for us an HTTP response, which is a special class in Django. And, and to use it, we generally need to import it. So in order to abort it, we're going to say that from Django dot HTTP import HTTP response. So now that we've imported our, our class and we ask. To return an HTTP response. However, there's still something missing in our code. And this is the argument. So this function needs to receive something in order to return that HTTP response. And what it's going to receive a request argument. So we're going as a client, we're going to request from the server to give us that HTTP response. And whenever, whenever this function gets that request, it's going to return for us this HTTP response. So as I said, this is, this request argument is going to represent that HTTP requests that they use roommate in order to access our web server. So if you want information about the request, we can look inside of that request object to get access to some other data. And we look into that later. And of course, we'll see an example of that in and throughout the course. But for now, know that you have to add argument. This request argument is an argument to a function. So now that we've written our function, there's, let's try to first run that code. And remember to run our code, we're going to type, type in Python, manage dot pi, one server. So we need to run our server in order to visualize the application and run our server on the whole project. So you don't have to enter hello and type and type math.pi runs, you know, your directory. Is it a project itself? And you're going to run the project itself. So if I go ahead and hit enter and copy that URL. And and now let's go ahead and paste it. You can see that we don't have this page that we've seen that we've been waiting for. We don't have this hello world that we've been that we've just written NOR function. And so why is that? That has to do with the URL routing. And we're going to talk about that in a bit. But just to give you an idea about what your routing is, we're going to fix this problem. So we've, we've talks about URLs previously, and we've seen that in Python, we have one by default, which is the admin. And in order to add this function that we've added, in order to visualize it, we need to add a path to it. And in order to do so, I'm going to add a new path aside from the one that is added by Django. And let's say I want to, I want it to be empty. So I don't want to add anything. And then what I mean by that is that whatever this URL is added, so I don't have any slash after it. We're going to get that function, that result of the function that we were waiting for. And so we have to use the abuse because the function is inside of use. So we start with the fuse and that the function which is hello. And then let's just give it a name. If we'd like to use it later on to identify it, we're going to give it a name. Let's give it a name of hello. And in order for Python to understand what do you mean by views, we're going to import from our project, sorry, from our application, Hello. We're going to import views. So reuse path. So we imported parts, we used admin, so important, admin, we use views, so we import it views. So one thing, don't get myself. This hello is the name of the function. So let's change, let's change the name for you not to get confused. Let's say greet. Okay. And if we change it to green, then when we go to your L's, we're going to say use that. However, we are going to still import from Hello from the hallow application, rm hello application. We're going to import views. And let's then, let's give it a name of greed to make them synchronized. And of course, the comma is a pooling. Specify that we have two paths, the default and one. So this is, sorry, this is the general walkway for us and this is the one we've just added. So now if we go ahead and reach for that. So if we go ahead and run our servers, Python and is.5 run server. And one note is that when you run this server, you write it on the application, on the project. So you can see that here we are inside of the project and not the application. So whenever you want to visualize an application, you'll run the whole project. So let's go ahead and copy our URL. And now, if we go ahead, you can see that we did at any route. And that's what we want because the path we wanted was nothing. Actually. We didn't want to add anything to it. And so you can see that we are returned by this HTML page, Hello world. If we go ahead and inspect that page, you can see that we have a head, a body, and inside of that body we have hello world. So this is a simple example of how to create your own custom homepage using jangle. And in upcoming lecture, going to see, we're going to talk actually more about URL routing. 9. URL Routings: Now let's talk more about URL routing and about URL patterns in general and how this portion works in conjunction with our youth. And so you can think of URLs as, when should Django actually return for us a response. A response can be an HTML page, which we saw previously in the previous video, or what URL is the user going to visit. And this is where now we begin to create some URL configuration sound sort of setting to tell Django when a particular URLs visited. And then this function should be run in order to return that particular HTTP response. So in our application, in our hello application, we have inside used apart a function agreed name, which will return for US response hello world. And in order for us to view that response, we went to URLs, the PI, which is inside of our project. And we imported that use file from that application and we reference it inside of our file when we go to route non-specific crowd. So by default, whenever we enter our URL lake, that is given to us when we run our server. We're on, we're going to call that function inside of use, which is here. And now, let's say that we have multiple applications. As we said, a project can have multiple applications. And so then how would jangle knew which I used to be meat? So say I important views from hello and from another application, let's say read. So we have multiple views files. And in order to avoid that, we can replace that statement by saying from hello dot cues from so from hello, from that file inside of our hello application, we're going to import that function called greet. And when you do that, just go ahead and write the name of our function here. So let's say we have another application. Let's say we add another statement from views that US import and the name of the function that we won't get confused by which use file with we need. And it turns out that we can do something even better. So we're going to create a URL, URLs dot py file for this particular application. So for the hello application, so Django has one-year alza pie that works for the entire project, which is this. But oftentimes we have each application. Contained in its own, contains its own Your else dot py file just for the sake of separating things it into different places. Because if you have multiple applications, each is going to do something interdependent. And so we can have each of these individual applications have its own. You are as a.py file to control the URLs that are available for this particular app. So we'll go ahead and go to the hello directory, which is here. And we're going to add a new file. And I'm going to be calling it URLs dot py. So what do we need to add to that your as a pi? So we need a list, a u r, l patterns list. The same, similar to that of the project. And inside of that list will be adding all the allowable URLs that are kept accessed for this particular app. And the way to create a URL is by first importing from Django dot URLs. So from Django URL's, we're going to import path. And now let's go ahead and create our first URL. And let's see, we want that. So we're just going to be moving it from here to here and adding it here. So this path is special only for this application. And this is the first argument. So I'm not going to add anything here. This is an empty string, meaning no additional arguments. And we're going to see that. What does that mean in a moment. And the second argument apart is what you should be rendering when you visit that euro. So I want to render on this function that wavefunction. Then what I want to do is just add the name, the name of the function here, which is great. However, we're going to first import from that directory. We're going to import use the fewest file in order to use that function. And so as I said previously, I'm going to be giving that path. The third argument is the name. And as I said, this can be useful later on. But the idea is giving any for a particular URL path makes it easier to reference it from other parts of the application. So later, when we might want to link to things or have forms that are submitting to different parts of the application. Give me a name of a path that can be useful too. And Of course, we need to specify that we want read from that file that's been imported. So this is the string, the empty string, meaning that no additional argument. And we're going to see that in a moment. And this is the function that we want, then this is the name of the function. Now, the last step in order to get all of this working for the first time is to go back into the project directory, through the project directory and open up your as a pi. So which is here. And this is the URLs dot py file for the entire project, for all applications are, might be contained inside of the project. And it just so happens that their response that already given to us by default called Admin, which runs a default Django application called Admin application, which we saw earlier. But for now, I'd like to add my own app criterion that I sorry, route. So saying that I would like to add my particular route, not the admin, but the follow-up that I've just created. So I'll go ahead and give this back, this path. So you have this path Hello. So whenever enter our, the argument with the hello slash, meaning the name of my application. And what I'd like to do now is to say that after I've included the hello, go ahead and include all of the URLs of the URL dot py or in my hello adaptation. So I'm basically linking these two URLs, URLs application files together. And so the command to do so is included. So we have a method called include, which is going to do that for us. So, okay, good. Hello, the URLs as the URLs of the hello application. And we're going to remove the statement. We don't need it anymore. And when to use the include methods to use, we need to import it from Django. So now there was a lot of steps just to get things started, but just to get a high level overview of how this is all working, I have a Django project, project one. And inside of that Project One, there's a sort three irs dot py file that decides what your eye can access. I can access the Admin, which takes me to the ammonification, which is greater by Django. And we don't have to worry about that just yet. But now I just add an another path which is the, the hello. So if I add to my l's Hello slash hello, and then to go to Hello application. And when I do that, I'm telling Django to look at the URLs dot pi inside of my hello directory to figure out what additional URLs. Um, which is here, what additional URLs I can get from there. So this is one master. The URLs apply that might connect to multiple different configurations that as this, that might exist well. And inside of the URLs, that pi of i application for the hallow application, I've said that when someone visits just the default route for this particular application. So if I just enter slash hello and I don't enter anything else after that, not sludge anything else. Just go ahead and run that greet function. That is one of my views, which is right here. So all that to say, we know that when that's done, I just need to say phi. So if you go to the command prompt and run Python managed up.py, run server inside of my project to actually start up a web server. And now I actually just want to copy that URL. And I'll go ahead and copy that inside my web browser. And I was going to ask slash hello to go the hello application. And now, as you can see, because we added that default route to, to the function that we just put into our views regarding that HTML HTTP response, helloworld, which you can see here. So just to recap as to what happened here. And when I typed that URL, that URL went to Django. And Django, Look at that URL and looked at that. You are asked a.py file and said, You know, and at times something starts with slash hello that belongs to the Hello application. And as you can see here, and the side of our hello configuration, we said that when we go to the default route, we should run that. And that read function. And the function returns this Hello World response. And now we can begin to change that response. You can begin to adjust what, what it is that used a pi is actually doing. Right now. It's saying hello world. Let's try to change. To change it to say hello Sarah, and go ahead and save it. And now if I go to my website and refresh my page, you can see that it it's changed so I can enter my files. And when I do that, general Django, notice that there's a change and it will be updated by the server with the latest code. Such that now I can visit slash hello. And I can say that the response of hello is what ultimately comes back to me. So here, hello, Sara. When I change my change it to the world and is going to be updated. Server, go ahead and refresh my page. It turns to hello world. So this is one way we could use the URLs that pie to measure enhance our applications in general. 10. Django Templates: Parameters: So in the previous video, we've talked about Django templates. And in this video we'll be talking about Django templating and its big cities. And in other words, we're going to be talking about Django HTML, which is HTML, but within the Django application. And you'll see what I mean by that in a moment. So this is the project we've been working on, and this is our application, Hello. And in order to to include Django as Shamil, I'm going to give you a small example before we begin. So Let's go ahead and try to create a function that instead of giving me Hello World, this template, let's say, I want, I want to say hello, Sarah. So let's go ahead and let's call this, let's call this function 0. And this function has an argument which is a request. And it's going to be returning upon that request. An HTML page, which is, which is going to be rendered first upon a request. And let's call that template. I'm Sarah, or let's say great GridFs demo. And let's go ahead and create that as Shamil file. So create great, sorry dot HTML. So let's go ahead and just copy paste the same content of index for now. And instead of saying hello, well, I wanted to say a looser. So now we have one last step which is adding that function that use functions is called Sarah to our path. So go to your other pi and at that path. So let's say whenever I type hello slash Sarah, I'm going to return with views dot Sarah, and which has a name Sarah. Just for simplicity. And save that. So this is pi, which is inside of hello, a Fei Hello application. And this is my, cuz this is my function, lousy ahead and open a command prompt and run our server. So Python managed the.py run server and click on Enter. Let's copy that link. And let's go ahead and paste it here. So we have to add slash hello gantry, our application. So this is Hello world. Now. Let's say I want to go to hello, however, Sara. So this is hello Sarah. Now let's go ahead and try to add. Let's say, let's say I don't want to see Sarah. I say John, So hello John. So I'll copy that code. And just instead of saying Sarah, I'll say John. And here as well. Let's keep now for now. The same, just for now. Let's save that and go now too. So it's great to Ansari and will be creating great order assemble. And it's out of control the shell, let's just copy the whole thing of grit and just change the name to hello. Yeah. And of course, we need to add it to our path. So copy and paste it. So you're sort of Zara and going to be adding John. And here. And here, John, just the name of our function. Now save that. Go to views, save that. And now let's go ahead and read. So Python Mantua Pi server. Or you could just refresh the page, doesn't matter. So and pasted and type in hello John. So you can see that we are returned with hello John. However, let's say I want to write a name. I don't want to go ahead and write a new function with the name of that person. And then go ahead and create a new template and then go ahead and add that new path. That seems so tell us to do. So. Here comes what we call as Django HTML. And as you will see, a shell pages that I can under using Django or parameterizable. So let's see what do I mean by that? So instead of adding a new function each time with a person's name, I'm going to just delete that. Or let's say, let's just give it for now. And I'm going to add and you function called greet. This function is going to take a request. Okay? And so whenever I type in my URL link, now to be adding in a moment, it's going to go to that greet function. And another argument inside of the greet function is the name that we'd like to add. So whenever I write here, we're going to be seeing as a variable. So whatever I write it, this link, I'm sending a request. And then we want to add to that request a variable. Which will be passing two. So here, the second argument is the name of that variable. So let's give it a name of name. I can call it whatever you want. And now I'm going to be returning rendered HTML page. So upon at Quest. And I'm going to go to greet dot HTML. So this is the template will be talking, going to, okay. And I'm going to provide that HTML template with, with some parameter. So I'm going to be providing it with a variable called name, which is going to take that variable name. So we'll add a comma here. Okay, so what I mean by here, let's just for you not to be confused. We can call it any name. It doesn't have to read it the same name for the variable. So let's say I call it greets name, doesn't matter. You can call it whatever you want. So will give me the giving that variable to that shell template, which is going to hold the value name which is carried with that request. So whatever I wrote in that link is going to be carried in that request and it's going to be put in that I shall template with the name, name. And now if I go to great, let's go ahead and just delete that. You don't need it. And that too. And that's just delete one. Ok. And now here, so as we've said, we'll pull it that variable inside of grit that I shall. So how might I add that to my HTML page? So using Django is ability to take an HTML page and put it like a template that we can manipulate. Django has added its own templating language, so say on top of that existing itself. And this will be able to render an HTML page that's actually have a variable inside of it. So it can have a loop or variables inside of it or conditionals. And you'll see that in a moment. But for now we're just focusing on parameters. So we'll be using that parameter inside of our HTML page as a variable. Going to inside of double curly bracelets at the name of the Bible. So, which is great name. So I'll go ahead and save that. And sometimes Whenever you visit IDA code in the bottom, which also Yoko detects the code of your template that you're working on. And so if I change it to Django HTML, it's actually the same, but the colors change. It's week. It makes it better for your eyes to see things better, especially Django parameters. So now that we've added that variable, let's just remove 0. And so now whenever I write and that variable is going to be printed here. So if I write sarah, Hello, Sir, John, it's gonna say hello John. And now the last thing we need to add is the path. So we've seen in the case of 0 or John, we've added a path under the name. But how would I add a path with a Bible? I mean, I can write anything inside of my, inside of that string. And so turns out Django provided us with a way to do that too. So we can, instead of writing a string, again, write a variable. I want that variable to be a string. And I'm going to be calling it name. So that string has a name, so that viable is a string type and has an a and call the greet function. And let's give it a name. Great. So now that name right here is the same one right here. So whenever I write a URL with a variable to the string, is going to be saved as a variable called name. And then upon that request, it's going to be going to go to views out greed. And we're going to pass that variable as a parameter and inside of that function in order to use it as a variable to our Greet dot HTML template. And I'll be adding it in my HTML content. And now let's go ahead and save everything. And if we go ahead and refresh our page. So it's still John nets. So let's say I want to write tallow Hello towel. So you can see I can write anything fff, Hello, FFF. So this, let's just go ahead and try to capitalize our names. So just to make it more. But looking so lean dot capitalize. So this is a function not, or let's say a built-in function to capitalize any string. So since name is a string, we capitalize it. So let's say a right-hand TLA is going to be capitalized. And again, of course. So now we've been able to use that general templating language to put variables inside of our HTML template. We were able to create an infinite number of infinite routes by using this variable slash hello slash name. And that name is going to be whatever we wrote. And this template is going to be more powerful in the next videos because there are another additional features that Django templating that shuttle is going to give us. 11. Django Templates: Conditionals: In the previous video, we've seen how we could put variables inside of our HTML templates. But we also said that the Django templating language is more, is more powerful than that. There are a number of additional features that the Django language is going to give us. And one of these features is conditionals. And I'm going to start by introducing you to a website that you may be familiar with. It's considered one of the simplest websites on the internet. It's an old website called as it's Christmas now. So is it Christmas, sorry, without now, not count. Going to give you know, if today is not Christmas and it's going to give you yes, if today is Christmas. So this is, this website story is very simple. You might imagine that it's really just an HTML page that probably contains a big heading that in this case, justice were no. But on Christmas Day just says the word yes. Now, how might a page like this be implemented? Well, one way you could imagine is on Christmas day, someone goes to that website and changes that Cheryl to yes. And afterwards changes DS Back to know. But we can be a little bit clever about this if you arise that now we have the ability to use Python logic when using Django. And so we're using those also in the Django HTML templates using that Django shameful language. So maybe we would add a conditional that says if it is Christmas, then print yes. And if it's not, it's no. So let's go ahead and see one of those features that Django has given to us, which is that conditional. And so in Christmas, and this is a christmas example and we'll be doing a Halloween example. So if you ask them if it's Christmas, so if it's Halloween or not by saying if it's that specific date or not. So first we need to create a new application. And to create a new application, we're going to on n inside of our directory, inside of our project. We're going to type in Python, manage data, start up. And let's call this up Halloween. So now the moment we hit enter on, we should have a new, newly created application inside of my project, of my project one for Halloween. And so you can see we have that we have all the files that we will be using. So let's go ahead and first grade the URL sub-par for this new application. So you are L's dot py. And as we've said, in order to create URLs, we need to import from the path, sorry, from Django dot URLs. And we need to import views from here. So from our directory, from the Halloween divert. Or we'll be importing use. And now create our URL patterns list. So patterns. And we're going to add or pass inside of our list. And now had to I project. And the URL of a new application to here, to my day URLs of my project. So which is called the Halloween, caused my applications for Halloween. And we'll be including all the URLs of this application. So inside of my salary dot URLs. So and of course, we need a comma to separate these paths. And one last thing we need to do is to go to the settings and add this new application, which is Halloween two. So now if we run, our server must be o phi. So now we need to create our function that's going to disclose this. That's going to be doing for us the logic that FX Halloween or not. And first let's go ahead and create that function, that index function. And which is going to take the request from the user. And now I might want to add some logic that checks whether S October third to one or not. And so how might I go about doing that in Python? While it turns out that there's a datetime module in Python. And you can learn about this by just looking up its documentation to know how it works. And the data module gives me access to things about the time, for instance. So first, I'm going to import the datetime module. And now the first thing I want to do inside of my function is to know what day it is. Such a do so and going to be creating the Bible called now, as in today, the current time. And I'm going to use for the datetime module, I just imported a function which is date time. That turn, that time that now. So now that the NOW function is going to give us the date, the current date. And now that we have the current date, we're going to return a rendered HTML templates. So upon request, of course. That template is going to be Halloween slash index. So this is the index.html. And I'd like to pass some variable into my HTML page, that index.html. And that variable is halloween day. So if now that month is equal to ten, as if if this month, the current month is October. And at the same time, now that they, as in today's day, is equal to 31. This is going to be true, as in today is actually Halloween. So let's just fix that, OK, to pass this parameter, which is going to give us true if today is the month is 1031 and it's going to give us false otherwise. Now let's go ahead and create our templates and add that index.html. So as we previously said. So inside of my Halloween, I'll be adding a new folder, which is called templates. And inside of my new my templates folder, I'll create another folder. Search is called How do we, which is the name of application. And now, and sort of how we were going to create a new file, which is called index.html. Sorry. Now, inside of my HTML file, I'll be creating this HTML document. Let's remove this for now. And let's call this webpage. Give it a title of a gallery. And I'm going to add inside of my body unconditional. And that's because of jangle. Jangle is going to give me the ability to add a conditional to my HTML template. And so I'm going to say if the if condition in Django starts with adding these curly brackets and then full background, a percentage sign. And so that effort, and we close that if statement by closing, adding the same percentage sign and causing that curly bracket. And add that if statement and going to inset, add incidents, these markets and the percentage side and statement which is endif. And now I can't add inside my if statement wherever I want you. So first of course, we need a condition. To our statement. So what might be my condition? So if you remember, it used up pie, we had this function, this index function, which is going to return for us that inlet index.html, which is right here. And it's going to be adding a parameter to this stoppage, which is halloween day, which is going to be true if it's Halloween. So if it's monthly, Stan, and instead you want so it's going to be true or false. So if it is Halloween, Halloween day, so if it is true, it has the same name. It's true. I'm willing to say yes. And now I add an else statement so I can add inside my if statement and our statement. So I'm going to add an else. And of course, I need to put it inside of these curly brackets, followed by a percentage sign. So else, if it's not true, it's not having I'm going to say no, it's not. So one last thing we need to do as added function to our URLs are pi. So we're going to add and default path. And that default path is going to take us to views dot index. And let's give it a name of index. So this is our, this is our function, and this is our index.html. So let's go and try to run this application. But first, you might notice that there are some things that seem off and use up. And one of these things is that equals sign. So this must be a W cosine. And because you are comparing these two objects, and we want this to region for us true or false. So now that they equal, equal to 31 is going to be returning true or false. And the second thing is that we need to indent. So you can see that here there's no indeed indentation. So be careful that inside a function you need. And then the third thing is here, it has to be important and ROM. So these are three things that seemed off in my code and that we need to fix. Or else our C and D would give us an and I will go ahead and run all project. So Python manage data, run server. And now we go ahead and copy that URL. And your hand and pasted here. And let's start with how we say you can see here that today is actually not Halloween. And that's why this case gave us a new So now we might. Want to make the snow bigger, a little bit larger. So one way to do so is using this static file, CSS. So let's go ahead and go back to my code. As I've said, maybe I'd like to add some custom CSS to this file as well. And we can do this conventionally the way we do it by just including SESS file. And that is what we'll be doing. A Django has a special build system for dealing with what are called with files. Files that are not going to change. So this S shell page is not a static file because it changes depending on the condition, whether it's Halloween or not. So if I visit on Halloween, it's going to be saying yes and otherwise, it's going to be saying no. And so this is a dynamic page. However, static files are files that don't change, like CSS. Css doesn't change whether it's Halloween or not. And because that file is unchangeable, Django calls it a static file, which means that Django is going to be a little bit clever about that. And if you think about static files, Django, you might want to store them in a special place where you can access them in a faster way later on. And we'll be talking about that a little more. But for now, just know that Django has features that makes it easier for us to deal with static files. Says that on the CSS file. And we, we add static files inside of our application that Halloween is by adding at the same way we've added that template such how we, I'll be creating a static file folder where I'll be including the files for my application suffers. I'll go ahead and knots inside of templates, inside of Halloween, I'm creating a new folder. It's called Static. And inside of static, I'll be creating a new file, which let's call it Halloween, which is the name of my application. And now inside of Halloween, BY creating a new file, which is, let's call it style.css. And now, before I get into style, let's just add is unknown side of a Heading one, just to make it make it a little bit Roger, larger. And in order to reference it inside of style that CSS. So in style.css, we can reference straight now though CSR, No. So let's give it this edge as Sean a font. Let's have a bigger font size and spirit, let say as sensitive. Let's, okay, let's start with the font-family. Let's give it a sensor. And now for the size, let's put it hundreds pixels. And for the text alignment will be centering it. And let's put the background color to be orange and the font color, or the color to be black. So now let's go ahead and refresh our page. And we can see that nothing changed. And that's because we did not link our CSS file to our index.html file. So in order to do that below title, we're going to add a link relation. Sorry. So we're going to be adding a link with a relation Stylesheet and a reference. So now the referencing is quite different. Fu, no actual CSS and rational. But because now Moore's I'm Django, in order to reference it will be adding inside of curly brackets a percentage and presenters, sorry, this name static first because a static file followed by the name Halloween slash style that CSS. So where is this five found? And lets go closer string. And now we're going to close that curly brackets and that way. So that is the way you reference that style, style sheets or CSS file inside of this HTML. So now let's go ahead and refresh our page. And as you can see, we get an error. And that's because we did not load our static file. And in order to do that, we have to add inside of our index.html load static to our HTML page. That way. Static files will be loaded to our HTML page. But just to recap on what we did. So here, I'm including this URL, which I can do. And Django is best practice is, let's just say it's going to be satisfied. So that's why we added the static. And I'm not going to exactly specify what the URL is, but I'm saying that it's a static file inside of Halloween folder called style.css. And Django is going to figure out what the URL ought to be. And this is better than hard-coding. The URL is maybe you cause you might imagine in larger applications, when your static files are, might be many. And you might move your static files to ensure different domain or a truck. And so in order to deal with that, the static keywords, that is that Django is going to figure out the URL that you have to and that you've tightened and it's going to replace it instead of Halloween slash style.css. Instead of this, this whole thing inside of the percentage sign and curly brackets. And now forgotten, fresh. You can see that we have no and which is, which has a background color, which is orange inside of that H1, and which has a larger font. And as I said in the center, and of course it's in black. So this is one way we could use. We can make use of conditions in Django. So you can see that we've created a very simple page that tells us if it's Halloween or not. And so in the next lectures, we're going to see additional features that we can use. Innerhtml, Django. 12. Django Templates: Loops: Until now, you've seen a couple of examples of what applications we can make using Django. We've seen the hello application in Django that can parameterize URLs that depending on the URL you visit, can say hello to Sarah or to John or anyone, or anyone in general. And you've seen the ability to use Django, Add, to add some conditions to be able to conditionally say if something is true, render this page, and if something else is true, render another page. Now, let's see. And let's use these features plus some additional features to begin to build a more sophisticated web application. Something like a to-do list that maybe I want my application to give me a list of reminders. And let, that lets me add reminders to my list. And let's, let's me view all of these reminders that are currently on my list. And I'd like to build this application one step at a time. So the first thing I need to do is create my application. So inside of my command, I am going to write any Python mans dot py start up. And I'm going to call this app reminders. So now we can see that we have reminders as a new application inside of our project, Project one. So first things first, we're going to go to project one, add this application. So Reminders come to separate the apps and we're going to add it to the URLs. Let's go and see that. And here, I'm just going to copy that and add a comma. And of course, let's just first change reminders. And here reminders. So reminders dot URLs to include all the reminders of the URLs. And so now we've written the path. So we've done that. And the next thing we need to do before we start with anything is to create that you're all stop.py file inside of my reminders application. And so from Django dot URLs import path because we want to use in the path. And from that As in front of the current directory. Import use NOW URL patterns. And here is what we're going to add. We're going to add our URL patterns. So go ahead and save that. And now let's go ahead. And you've used the apply. So let's go ahead and create our index function. So request. And now inside of our function, let's say we have a list of reminders that we'd like to post nor page. So let's say that the first reminder is coal. Mom. Let's say that the second reminder is class at eight. And let's say Exam on Tuesday. So these are reminders and I'd like to print them in my page. And to do so, we're going to return first and then show page which is rendered upon request. And let's call this national page reminders slash. So then it is index.html, which is inside of the reminders file, will be creating a moment. And we'd be passing a parameter which is reminders. So that remainder is the reminder that we have right here. So now the next thing we offstage nu is create our first templates five. And inside of our templates file, I'm be creating another folder, sorry, folder, which is Reminders The name of the application, then a new file which is index.html. Now inside of my index, I'll be creating the head and the body. So let's be naming my page reminders. And I have that parameter, which is a variable, which is tasks that I can use. So how can I loop through tasks? So as I've said, we want to have a page that gives us all the reminders that I have inside of that HTML page. And so I'd like to add these reminders in a list. And let's say I want to add them in an unordered list. So to create an owner, an unordered list, we use the UL tag. And now I'd like to add a list item for each reminder. And so what I really want to do is to loop over this list, which is reminders, to look into all the reminders inside of it and create a list item inside of a for each of these reminders. And before we used to use an if statement in the previous video, and likewise will be using the for loop. And in order to loop over that list of tasks. So the way to do that is adding a four. So four reminder in reminders and all reminders variable. So this is the environment that we've passed on and this is a new variable that we've created, which is an item inside of our reminders list. And so for reminder in side of our reminders list, we're going to create a new tag, a list item tag. And inside of it prints that reminder. So we'll go ahead and close that. And so that's, that's how we do it. Now. And here you can see that we ended our for loop by adding an end for statement inside of curly brackets and percentage signs. So now, if I go ahead and save that, and we have to add the task of our function or index function. So this is the default path, not adding anything. So we'll be adding, we going to use the index and thus give it a name of index. So we have our URLs that we have all used up by the function. And we have our template, which is right here. And now we can go ahead and run our code. So in order to do so, I'll be typing Python managed at Pi run server. So I'll be copying that link. And now I copied to here. Let's add reminders to go to that application. And sooner you can see that we have now a list and which is, which has those list items, those reminders. Now let's go ahead and just make it literally gone looking. So let's just add here reminders and a H1 heading. So that made our page most little reminders and then the reminders. And so we can remove those dots in two ways. You can either change this to an ordered list and we can do something else, which is having a, a for loop counter. So n, therefore rho of Django, we have something called for-loop dot counter is going to give us the culture of the loop. So for the first reminder, this is our first two. For the second reminder, we are in the second loop. And so I'll go ahead and save that. And we can see that we have now 123, although we have that put points, because this is an unordered list. So we can just remove that. Actually. I just wanted to give you an example on how to put these numberings, these counters using that form loop dot counter and without having to add an unordered list. Okay? So, so let's assume that for now. And so this is my page. And we're going to see in the next video how to add additional pages to that page. 13. Django Templates: Inheritance: Now that we've seen how we can use loops, conditions, variables inside of Django templates for going to see how we can also use inheritance. And we'll see that through our previous example concerning reminders. So reminders, if you recall, we want to make a page that shows all the reminders that we'd like to have. And then if you want to add some new reminder, we can add and you reminder to our list. So this is my HTML page and it has that for loop. And inside of that for loop, we're going to add inside of that list item, each reminder in our list. And now to add and something to our list, we need to go to a new HTML page, which let's say is called app.all HTML, in which we'll have different things in here at the front for loop. So we won't have a for loop you want to have, we would have something else. So added at Shamil, sorry. Let's see. So a new file and HTML. And before adult HTML, we're just going to couple the two here. I am going to change reminders too and new reminder. And then we're going to remove that. And I'm going to adding something else. We shouldn't be seeing Iran. But now you can realize that I just copied here the, the content of index.html to this new add HTML. And then time you find yourself copying and pasting. This should be another area where you should start to think that there's probably another way, a better way to do this. And so if you want multiple different HTML pages that would display similar content, we need and the same HTML for all those different pages. But now in the world of Django, We have the entity to use something called template inheritance. What will be doing now is going to create a new HTML file, which is called layout. So Layout dot HTML. And this file is going to be my main file. And I'm just going to, and, and before that the index that HTML and HTML will be inheriting from that layout.html. And so we're going to inherit the structure of that layout.html page. And these all other beaches would be taking those. And as I mentioned before, the only difference between adult HTML and index.html is the content of that, which we can call the body. So let's just go ahead and go to layout and paste that. What we can do using inheritance is remove that and just write in here. So I can add here a block body. So block. And I'm going to be calling it buddy. And what I'm saying here is that this block, this body block, might change depending on which file we're using. Adult HTML or index.html. The structure won't change, but the content of that block, this block called body, might change. So now if I go ahead to index, I don't need to rewrite head and all these information. All I need to do as though to that blog, which is the body. And so whenever I write here and it's going to be replaced and here. So whatever is inside of that block is going to repeat placed here. However, I decided to write it in a different page. So this is now an idea of Inheritance. I'm inheriting from the layout, this HTML, HTML template. So I'm basically saying inherit from that layout.html template, except in the body, I would like to include that. And now an adult that this channel, I'll be doing the same thing. So here we have that block and it's the body inside of it. So now, one last thing we need to do in order to tell Django than converting these to HTML templates from Mahout is by adding and extends. So adding an extense and of course extends what? It's going to tend that layout.html, which is found in reminders. So Reminders slash layout.html. That's how we tell Django that I am inheriting from that page, these body that blocked by. And I'm going to add it here too. And that its shell. And now let's go ahead and save it. So that's how you use inheritance through Django templates. And in the next video, we're going to see what are we going to add the block of that, add HTML. 14. Django Templates: Forms: So in the previous video, we've been trying to create an application called reminders that's going to list for us all reminders that we have in addition to that. And it's going to let us add additional reminders to that list of reminders. And in order to do so, we're going to use forms. So that form has an input which is shadow type text. And maybe it's input field has a name fold reminders such that I can access that data adding and the input, let's say another input type submit. And I'll give it a big heading that says add reminders. So here is my edge HTML template, that ingredient the previous video. And now, as I said, first, let's go ahead and add our title, add reminders. And we're going to create a form. And that form is going to have an action and a message and revealing to see what each of these mean. But for now, let's just remove that. And as I said, instead of all form, we'd like to have an input which is of type text, and another input which is of type submit. And now what I'm doing is giving the input a name of reminders. And so now I'll be or maybe let's just keep it reminder without a now, if I go ahead and run my application. So Python, man shot.py, run server and click on Enter and copy that link. And to our web browser and just pasted here mu. I actually just refresh. We can see that now we have the speech. However, you can see that we have a URL that takes us to. So let's see here I add sludge, which is the URL of reminder slash. Lepage is not felt. And that's because we did not actually add it to our or else the pie. So let's go ahead and add that path. So path and reminders, sludge will be going to use that. And, and let's give it a name. And so what is use that, that is the function that we have inside of us. And so let's go ahead and create our function. And, and It's going to have a request, of course, as an argument. And returning that HTML page, which is rendered of course, upon request, that HTML page is at HTML, which is found inside of the file called reminders. So now we added our function, we added the path, and we have the ad that HTML. So now we go ahead and refresh our page. We can see that we are returned with the page, with that abdomen you page. So let's say I type in here, Hello. And cement. You can see that nothing actually happens. So I'd like to do is let something happen whenever I submit, whenever I click on submit. And so if I go to natural minders, Hello, is that added? So what I'd like to do is actually add reminders to are my current list of reminders. And nor do you do that. We need to create a form. But first what I'd like to do is I don't want every time to just type in and then go, and then to go back, I need to remove that. I need to add some link that gets me to navigate between both pages smoothly and not having me to add the URL. And so let's go ahead and see how we can do that. So what I can do is add a link inside of my index page, which has hyperlinked, which is going to take me to slash, slash. And thus, whenever I click on, let's say add new reminder. So now go to that page. So now if I go ahead and refresh, you can see that we have a new added link. So now if I click on add a reminder, you can see that we are taken to that page, to that reminder page. However, what I'd like to do is when I add something on, let's say hello and click on submit. I'd like to do is return to the main page. And and to do so, we can actually inside of our inside of our adult, as Cheryl said of the form. What I'd like to do is whenever I submit that form, I need to have an action. And so now let's go ahead and add an action. And that action is going to be executed whenever I submit my form. And our action is to go to reminders slash index. So nasopharyngeal index. So honest session matters because as we said, on the index, which is the default page, doesn't meet a parameter URL, as you can see, so we don't need to add anything. So now. If you go and refresh our page. And let's say it live on. Let's say hello. Hello. Can submit. I am mature to money page. So you can see now that I can navigate smoothly between the two pages without actually having to add your else. Can see that whenever I type in Iran take into that sludge. And now Let's say I add anything and just submit. You can see that I returned to the main page. Now we can on knowledge something that we have, a variable that is holding whatever we are writing quadrilateral or submitting. And so what we'd like to do is to use that variable in order to add it to that list. And the way to do so is celtic, we're going to see in a while. But now there's something I'd like to I would like to get, pay attention to action is that if you notice that in form index.html, and we have this link right here. But suppose that on, throughout my application, and whenever I add more and more pages, I decided to change the name of my use of that URL. So let's say I want to change the name from add to something else. And so in that way, I have to change that you are all right here to the new one in all the pages that I use that link in. And you can see that this is going to be tedious. And you're going to the searching through all the links that we've used, that we've used that reminder slash added. But you might see that we can have something that's more efficient and less time consuming, which is to let Django actually figure out the link for us. And we do that. We're going to use the name of the lake. So or the path. So here comes the roof of the knee to going to call that name, called a path by its name. So you don't have to actually specify that parameter of that link. We just call its name. And the way you do that in general is just by adding in between curly brackets and, and of course, percentage sign. I'll be adding w1, the URL with the name at. So. And that way we have the name of that path, which is, and which I'd like to call. And Django was going to figure out that actually you are phony without me having to write it and amp preceding that. And let's go ahead and see if that actually works. Let's just also do that for an adult HTML. So instead of adding slash reminders between percentage signs and curly braces, I'll be adding your own mutiny index. Now. I just go ahead and refresh my page. And click on add new reminder. You can see that we are taken to that page, that page. And now let's say I add SSS and submit, I am returned to the main page as well. So you can see that that is more beneficial for us later on whenever we own rite and other programs. Now, one thing we can notice that is that for example, inside of our hello project application, sorry, we have actually a, you're inside of the earth by a path and within the index. And the same goes for the Halloween application. We also have a path at any index. And so in order for Django not to get confused by which index the mean. So here for example, we added your l index. So an order for wrangle too, too on what to know what index remain. So what we'll be doing is at an airplane for these URLs. So if we go to URL, so pi, so in order for Django not to search all the URLs dot of this project. So your odds of Halloween, and if hello and reminders will going to narrow it only search the URLs of this application by giving an app name and as given an app name of reminders. And so now if we go to app dot HTML, so go into del Django to search for a URL that has an index. However, I don't want to search all the impasse with any index. I wanted to search only with the URL's that have the app name inside of the application with App name, which is called reminder. So reminders fall by column and then followed by the name of the path. So in that way, as I said, Django is going to only search for URL's that have an unclean called reminders. And it will save that and see that too. And here also, let's add reminders column and then followed by add. So now if we go ahead and refresh our page, and now go ahead and click on, add a new reminder. You can see that it still works where given that adult HTML page. And let's try to submit and three are taken to the main peach. Now, what I'd like to actually do, as I said before, is to whenever I write something, I'd like to add it to my list. So let's see how we can do that. So I'm going to be doing, in order to add that to my list of reminders, I'm first going to add a method which is equal to post. And we've already seen a request method. Again. Every time you type in a URL or link, you go to another page, the request method implicitly associated with that is called get, which just means I would love to get a particular page. Anytime you submitted data that has the potential to change something inside of my application by seeing steed on the list of reminders inside of my application, then we generally use a different request method called post, which is right here. Post is generally used to submit data. So this post method is going to give us the ability to send data via a different request method to my dropped. So let's go ahead and see if that works. So I'm going to be adding a reminder. Let's say it. And I'm going to, I'm conscious of net. So we can see that now I've got an error. And this 43 inside the parenthesis means that the response code, the key that came back. This is an error from jangle, that means forbidden. And that means I don't have permission to do this for some reason. Now, why don't I have permission to submit this form? It says CSRF verification field. So CSRF stands for cross-site request forgery. And what that means is It's a security vulnerability that is inherent in some forms if they're not designed in secure way, meaning that someone could afford a request in a particular website using some form on their own separate website, for example, my imagine that if someone on different website might trick the user to submit a form with its data to our health reminders function that adds a new reminder to their reminders this. And maybe that's not a big deal for reminders. But you might imagine in more secure context, more like a bank for example, they might have a phone and their website for the information of your bank account. And so cross-site request forgery. Someone else on form could trick the user to get data that he should not have. And goes to the bank's website to say, I would like to transfer money from this user to another user. So we would like to be able to design forms that are not vulnerable to this security vulnerability, that don't allow requests to be forged by another website or user. So how can we go about doing this? Well. One strategy that we can use in order to deal with these sorts of attacks is to add into our own forms a hidden cross-site request forgery token or CSRF token. We just, which we just, which is just a unique token that we were using for each session. So every time we enter that form will have a new token. So every time the user, or every user, that sum is that form is going to have his own unique CSRF. And our web application is going to check to make sure that that token is indeed valid. And if it is valid, then they'll allow the form submission to go through. But this means that for that user, that forgery user won't be able to access those information because he doesn't have that token. So so they would fail to check for CSRF request validation. And so just happens that Django has CSRF validation turned on by default. And it's done via specific data on known as Django middleware, which we saw in the seconds settings Debye of our applications. So you can see that here we have jangle that went lower, dot CSRF, Dutch CSRF, view, medulla. And middleware first has the mood in Django to be able to sort of intervene in the request response processing of a jangle request. And so here are a bunch of middleware that's installed by default inside of Django application. In terms of making sure that we have variable features have into this request response process. And one of those is the CSRF view middleware. This feature of jangle that allows it to make sure that our requests, whenever we're submitting data via post, doesn't have a potential to change this application in some way. And that we need to have a CSR validation in order to do that, which is going to create a unique token for that specific form in order to authenticate the validity of this form. And show to make sure that this, for this form came from the user himself. And in order to be able to add stoke int or Django to our page. Django has given us a way to do that. So instead of my form, I'm going to be adding a CSRF token in that way, can retrieve curly brackets and a percentage sign. And so now let's go ahead and save that. And go to add a new reminder. And let's try to add a new reminder. And now you can see that we don't have that anymore. And if you actually try this, you can see that if you get that CSRF or try running your rerunning your city server, if you're still persisting with that error. Sometimes it glitches and learn computers. So you just drive in finite-dimensional program server. So you can see that we're able to submit that form successfully without having errors. And so just to visualize that CSRF token, where you go to that page, you can see that inside of my form, we have an input which is hidden and listening as CSRF middleware token. And this value is the token, so it's unique for that four only. So if another user goes ahead and opens that form, he's going to have a totally different value for that token. And so that way we validate our form. And each user has his unique CSRF token. And as I said, we're able to smoothly just u to the mean list without having any problem in somebody that phone. And so what's left for us to actually do to implement that reminders is to add those reminders to my list. And we're going to see that in a while. So in the next video, we're going to see how we can use another type of form. And we're going to be introducing that in the next video. 15. Django Forms Part 1: In the previous video, we have seen how we can create forms using HTML. So using Django templates, which are HTML templates, we've been able to create forms. However, we're going to see in this video that we can use Django forms, which are much suitable for us. And we're going to see why. So if we go back to that application we've been working on, we're trying to create an application that has a page that contains all the list. So the reminder, let us up reminders that we have and we tried to create a page that's going to let us add new reminders to that list. And so we create a form in that HTML page. And you can see that we create in this form sort of from scratch. I created an input field whose type is text and whose name is reminder. And an another type. And I'm sorry, of type submit. But creating forms that happens so often in the world of web programming. Oftentimes with many different fields that you want, might want to change over time. But Django has added a number of ways to make it easier to create forms to validate the data inside of the phone, just to make our lives a little bit easier when it comes to dealing with interacting with forms. And so now we'll explore the way you do the same thing. What we wrote, wrote here. Just works. We submit the form and everything works out. But jangle has the ability to create Django forms for us. And I will tell you that as a abused apart, I'm going to be writing and top of my page from Django. I am going to be first importing forms. And now I'm going to create a new class to present this form. I'll be creating a Python class and let's call it u reminder form. And of course, no spaces between San Yue reminder form. And because this war is going to, sorry, since this class is going to give me new reminder, it's going to inherit from forms, that form. So forms, thoughtful. And of course we add a column and then indentation. And now inside of this class, I need to define all of the fields that I would like for this form. All the inputs that I would like the user to provide. And so I want them to provide the name of that reminder, which will be a character field or a char field. Meaning I want the user to type in characters. And I can give this a label. So you, you and your mind. And the will to do so is by adding a new variable reminder. And I'm going to type in forums. And from forms, I'm going to get char field. So character field and I'd like to this guy to get this char field. An argument which is labeled and stable is new reminder. And so now this is my form, which contains a char field where the user can write down the name of the new reminder. And now in order to add it to our eggshell page, we're going to, when we're going to render that added a shop page, we're going to pass in a parameter, which is that form. So we're going to pass in a parameter, a variable which is formed, which is going to be a new reminder. And form. So new. And so that's the way we create a new form. So this is the class, and I'm creating an object of that class and you form of that classroom. And so now, all we have to do four. If you save it first and go to edit HTML. And instead of writing this input type text, I'm just going to plug in that form. And that form is the same form we would she passed in as parameters. And so what I didn't hear as just using those double curly braces and seeing plugin that form here. And that will automatically take care with Django, will generate the necessary HTML to make that form work. And so now if we go ahead and run our application, so Python managed that.py, run server and copy that link. And you can see that we have an arrow. So let's see what the error, you have Attribute Error module Jenga that forms has no attribute for. And whenever you get another, try to understand where you got that error decoded. So where did you cause that to happen? And so you can see that we have an indication that it is in line five of our views dot py. So you go to line five. And you can see that we need to capitalize that form. So forms, that form which is capital. And now let's go ahead and save that and copy that link. You can see that our error has been resolved. And now all I have to do is just copy that link and paste it here. And I'm going to go two months. Now. Let's just put fish, fix that. So it's with a small ONE. So, okay, so reminders. So now if I go ahead and click on, add a new reminder and can see that I have this char field, which has the label, your reminder, and the submit button. And I can type in anything. And you are getting to that pain may be. However, that new reminder is that added. And we're going to see how to add that. However, in the next video. For now, I'm just really focusing on that phone. But for now, maybe I want to eventually make upgrades to my application where in addition to having that new reminder. So that chore field where I pass a new reminder, maybe I also want to be able to specify a number indicating the importance of that reminder. I can additionally give this form access to an important variable, which is an integer field whose label is importance. And so let's go ahead and see how it can do that. So let's create a variable called importance, is going to be a forms that integer field. So let's just fix them. Forms that integer field. And I'm going to give this integer field label of importance, sorry about that. So importance is the label of my, of that variable. Now I can add constraints to this integer field. So in order to make sure it's valid, I can give it a value, maybe one, minimum value of one, and a maximum value of ten. So the range is going to be between 110. So min value to be one and max value. Of course it varies by a comma and a max value of, let's say ten. So now if I go ahead and save that and refresh, I'm going to refresh them. I can see that now. And I should having a char field, which has a new reminder. I also have an integer field or for the importance. So now let's say I added and you reminder it. And let's see. I add an importance of which have the value which is outside the boundary that we specified. So this added 11. I may click on submit. You can see that Jang gave me that this value must be less than or equal to ten. So I cannot submit that form unless I have that importance value inside of my constraints. One more thing is that say, I don't type in any value and submit. I have to fill them. Applaud that reminder. So I can add just submit one field. I have to submit all the fields that are inside of my form. However, that's not only the case. I can tell Django that maybe I don't want this field to be of the gallery. So maybe I want I have this text field that the user can type in a description. However, that description is option. So it's optional. That means that I could submit my form without having to write anything inside of that description and see how to do that. One, real reach models. And that's going to be in the few next few videos, hopefully. So one last thing. Maybe you want to put your peers in a way that's different than that. So I don't want them to be aligned horizontally. And what I can do as inside of my adult HTML. And instead of just writing form, I can add formed a parameter which is as p, which means as paragraph. So now if I refresh, you can see that now my fields are in a paragraph form so they are below each other. Either thing I can do is I don't want it to be as a paragraph. I wanted to be as an unordered list. And so as you well, and now if I refresh, you can see that these are in bullet points and below each other. We have submit. So that's one way we could use forms instead of just using HTML and HTML, but using those Django forms inside of our applications in Django. And one important thing that we need to change here is that the action should be now at, since we've added that. So we'll be using that add function if you stop by. Whenever we submit that form, we are going to let it go to that add function in order to work with it. And in the next video, we're going to be seeing how Bill me adding that application, sorry that reminder to our list of reminders. 16. Django Forms Part 2: In the previous video, we've seen how we could create forms, Django forms that are natural HTML, and how we could just implement them on through double curly braces. Inside of which we add that form, that variable, form that be fast by and our views dot py file. And we can just let these fields, a turf fields and integer fields, be represented as an ordered list or a paragraph. Or just by default the way it was. And you can see that we can add a new reminder and add importance from it. And so the importance, as we said, is bounded between 110. And so this is what we call, again client-side validation. The server is going to get any data this webpage has encoded to know what that value is. And it's going to constrain me to make sure that I submit something that matches that form. But in general, when you're doing some form validation, when we're making sure that forms are valid data, we're going to somehow sure, make sure to include not only could client-side validation, but also Server validation. So we want to also check on the server to make sure that the inputs are valid as well. Because there are many users that why would we want to be able to use the one is that it's able to disable that side of the client validation and have a request without all the client-side validation. And so the validation on the server side is more up-to-date, might be more up-to-date from that on the server side. And Django forms will make it easier for us to do both of these things for the client-side validation and server side validation. So how does this work? How do we do that? Well, inside of use up i I'm going to we'll first, before we get into it, we know that in order to get that add function, we know that it's called in two years, one in by typing the URL reminder slash app. And secondly, it's just by clicking on the link for Add a New remote. So if i post data to the speech by using the post request method instead of get, then that means that I'm submitting the form. So I want to now to submit a new reminder to be added to my list of reminders. So I'd like to add a condition for that. I'm going to add a condition here that says, Sorry, inside of, inside of the add function. So sine going to add a condition. And that condition is if. My request, that method is equal to post and not get. So then I'd like to do what I love to do is process the result of that request. And the way to do that is by creating a form. So form variable and which will be calling the view new reminder, form and request, which is the argument quest opposed. So we want to get the data that we have that we have entered by submitting that form. So now what I've done is created a new variable and created and you form. And that new form is going to hold all the data that the user has submitted when he submitted his form. And so you can imagine taking this manually, but I can just call if form is valid. So if that form is valid, so if the data that we submitted into that new form are valid, which ultimately means that the user has submitted valid data. I'll be doing something which is creating a variable called reminder. And that variable is going to be taken from that form. And to do so, that form, that cleaned underscore data. And I would like to get the data which has the name remind. So and are smaller. Because once you have the same name as the one inside of our class, our form club platform. And now after that, what I'd like to do is add that new reminder that I got from the user and to my list of reminders. And we did do so is to first make that list on reachable for that function and go to doing so by adding it outside of the function. And so now I could use it in both functions. And now all I have to do is appended to that list, the reminders, that band, that reminder. And I'm going to save that. So that's what we do. If the form is valid, the form is valid. We'll get the data from the phone, get a data reminder, save it inside of a variable called reminder, and add it to my reminders list. But else, if the form is not valid, so what should you do in that case? Well then I should return that at that HTML file again. But instead of returning a new form, I'm going to return the old form, the same form for the user to see. Where did he submits something wrong inside of this form. So here I'm going to add an else. And so else as that form is not valid and going to returning. That rendered HTML page. And here, reminders slash dev channel. And I'm going to pass by and you form a, sorry, another new form is same form that we are having the same data that the user submitted, which is which we initialized after that f, which is here, which has all the information that the user submit. And so now I'm going to save that. And let's go ahead and check that page. So now if you go here, if you go ahead and type in it, let's say and add an importance of 11, for example. At present submit. I am going to get that this value must be less than or equal to ten. But let's now imagine a situation where the client and the server are validating different things. Maybe, maybe the important aside, my variable is from one to ten, can only be now. So now it's changed to be from one to five. So that's now the valid range for on that form. But this client is still on an older version. So here the client is an older version. And so now let's say this page, this client, which is still on an older version of this page, doesn't know that we need to be between 15. It thinks that maybe if we add a priority of it, it's still valid and we're going to pass in client side validation. But now if I present submit, I get that the servers want to process it and that servers going to give me that an error which says that ensure that this value is less than or equal to five. So ensuring that this importance value is less than or equal to five. And so this is why we generally want both the client and server to have validation to make sure that the data, ultimately, we ultimately get is going to be accurate. And it's going to be cleaned matching wherever specification sent out when we already, when we were creating that form for the first time and so forth. The purpose is for now, for some person says, sorry, will not be worrying about that. And so for now, let's just go ahead and remove importance for now. But just know that if you care about virtue reminder, importance it is. We could add that importance viable as an additional field to our webpage. And you can also add multiple fields to this form as well. So now we've added some application logic to our web application to check if it's valid or not. If we take a look to what the add function is doing. We see that if the request method is post, we have a new form which has all the data that the user has submitted, saving it in that form variable, and check to see if this form that has been submitted is valid. That they actually provide the module with all the conditions inside of that form, like the example. If so, then we get that reminder and appended tower list. Otherwise, if it's not, go ahead and render the same reminders application page, sorry, with the same form that you submit it. And in order to give you another chance to to resubmit it with the correct values. Making verification validations to their own form. Otherwise, if the requests are that wasn't post was get. So in that case, if the user just right, tried to open that page rather than submit data that we're just going to return for them and that page with an empty form. So Zara and just remove that and thus try to enter a new, remind you. So let's say it and go ahead and click on submit. And you can see that when it's submitted, that reminder actually is in that list. It is appended. However, we need to go back to that main page. So let's go ahead and just add a link that takes us to the main page. So inside of ADA at several, let's just add a link that takes us to the index page. And the will to do so is going to be writing the name of the URL and Django is going to be figuring out the actual URL for us. So the happening is reminder and the URL, the URL name is index. And that will close that and Let's return to reminders. So now if we go ahead and refresh our page, continue and click return to reminders. You can see that we have two 0s actually because refreshed our pitch. So it took that twice silicosis mad and try to add another reminder and submit. And you turn to the main page and you can see that at once. So that's just because we're refreshing the page to a tricky choice. So, and that's how we use forms in order to add things to our lists inside of different pages and to ensure validation on the client side and the server site. 17. HttpResponseRedirect reverse: In the previous video, we've seen how we implemented the reminders application. How we created a page that shows us a list of all the reminders that we have. And we can just click on a link to add a new reminder. And that link would take us to a new page where we get this form and we fill out all the necessary information inside of that form and submitted. And so when we submit it and get back to our main page, we can see that now our list has the new reminder that we've added. In this video. We're going to add a little more additional features to our application, which are the HTTP response to direct and reverse. So this is used up by, and here we have our list of reminders. And that's the form, the class in which we have a variable called reminder, where the user is going to write inside of that Chore Field, character field. Then you reminder that he'd like to add. And so this is the index page which has a list, sorry, the add page and index page. And so the add function is going to see if that request dot post is both, meaning that if the user submitted some information, we're going to see that information in a new form. And that form is valid. I'm going to get this variable reminder and see if it's valid. And they're going to append it to my list. And that list is outside of the class and all of us at all of the functions in order to access it. And if there was no information. First of course, if it's Well, if I'm going to return our benefits, not sorry. I'm going to return the old form with the OEM form that he submitted and if the user did not actually submit information. So if their requested method is get, I'm going to give the user and you form for him to fill and submit. So and we saw the page. So we have to first run Python managed dot py run server. And so that's just a recap of what we've done so far. So now, my African Summit, I should receive the link that comes with them. So HTTP thing pasted here. So of course, page not found because we did not specify the applications. Reminders is application. This is the page and this is the list of reminders. And one thing we notice is sorry, if you want to add a new reminder. And let's say I want to add goto, the germ and submit that form. I'm returned to that same page. And if I want to go and see that list again, I'd have to click on that link. Give me that page. But this wasn't quite the behavior that I might have expected. Maybe I want that maybe after I've submitted that form to a reminder, I would like to be redirected back to this page as well. So without having to click on that link, which is right here. And so it turns out that Django makes it easier for us to be able to redirect users from one page to another. And in order to do that, after we add a new reminder to my list of reminders, I'm going to return an HTTP response redirect. So, and so the HTTP response with direct is going to redirect the user to a particular route, as I said. And so we're going to add it after we append the new reminder to our list of reminders. So I'm going to return a HTTP response, redirect. And and I could just through director too, just like slash reminders, which is my index page, as you can see, instead of my URLs. So It's by default the reminders. We didn't add any additional route. So when we go to slash reminders, as we saw here, without the add, of course, redirected to this page. But of course we don't want to add, we're not going to try to add some hardcore URLs into our application. So we don't want to add slash reminders. I want Django to figure out the URL. So let me give you the name of the route and go ahead and reverse engineer what the route is from that. And so in order to do that, we can use the function called reverse built into Django and say, so instead of that, we are going to be saying so. So reverse. And instead of reverse, I'm going to specify reminders which standing app name of my URLs dot py. She's right here and followed by the name of the path that I would like to go to. So the index, I would like to go to that index page, which is the name of that path, my default path. So now this, as I said, is to say to Django, figure out what the URL or the index your ELL is for that Reminders app. And use that URL that will ultimately redirect me. Whenever I submit that. Whenever, sorry, I append that, I'm going to be redirected to that page. And so we need to import also from Django that SCTP, that HTTP response redirect. So I'm pouring SCTP response redirect. And of course, from Django, I need to sort of Django dot URLs. I need to import reverse. So in order to use them, we have to import them. And I'm going to saving my page. As I said, I've important both of these and now the effect of this is after I submit a new reminder and added to my list of reminders, I'm going to be redirected back to that index page of my reminders application. And for good measure, I'll go ahead and start off my PR. So first of all, I'm going to make my list empty. So let's begin with having an empty list of reminders and go ahead and run. So I'll refresh my application. So now as we can see, our list is empty. And so let's add a new reminder. And so let's say coal mom, and click on submit. And as you can see, me are the moment I clicked on submit, I'd have been new directed to this page. Automatically. I didn't have to click on that link to get me back to the main page. You can see now I list has 1D module, which is called Mom. And so now let's say I were to add another reminder. I can just add it. So Summit and it's led. Now to make our application better, we can do one more thing. So which is when one, our list is initially empty, we can do something to our code, make it a little bit better. So we can say that if our list was empty. So we're going to see that by adding inside of curly braces and percentage size, Mt. That's the way I see. If, if that list is empty, I'm going to add another list item to my list, which is no reminders. And if it's not empty, that won't appear. So now, in order to to have an empty list, if I refresh, it is going to be the same list. So in order to have an empty list, again, I'm going to rerun my server. So the moment I will run my server or the information that I have added this gun. So Python managed park run server. So now if I go ahead and refresh my page, I can see that I have no reminders. This is my new list. And if I go ahead and we can see that now the new reminders is gone. We don't no longer have it goes one is it's no longer NT. And now I just have my new first reminder, which is call mom. So now let's just go ahead and remove that thing is that reminder by going to othered as Joel and removing. So now, if I go back to my page and let's see, I add back, you add a new module and so on. So we can see how useful this HTTP response to direct and reverse or two are useful to our application. And so instead of having to click between two links, I can just use that reverse and that shouldn't be responsive, direct to automatically be directed. And so that's it for that video. 18. Sessions: In this video, we are going to be talking about sessions. So in our application, we've been working on reminders. If you go that C, Let's say Joe webpage. So this is our webpage. We can add reminders here and say, go to the supermarket, and so on. However, there is still one big problem with the application that have booked. So till now, you can see that we can add reminders, you can add a new form and submitted and so on. However, as I said, there is still one big problem to the app, to that application, right? I think goes back to the idea that we're storing reminders inside of a global variable. This variable is the entire application has access to, which means that anyone who visits my website is going to be able to see the same exact list of reminders. And we can simulate this by imagining someone else visiting this URL. So I can simulate that by going to Google on magneto mode. So if I go ahead and copy that, seem like you can see that now. First of all, they will go and do a slide. Different personas interacting with the page and going to the same your L. And so you can see that as a goo to that same URL. I'm given the same exact list of reminders, both me and another person, C the same list, because there is just one reminders variable across the entire application that is shared among all of the requests that come in that particular application. And that's probably not what I want when I'm dealing with something like a list of reminders, I probably want to be per-user such that every different user visiting that page have their own list of reminders as well. And so in order to do this, we'll introduce the concept of sessions in Django or web more generally. Where sessions are a way of making Django to be able to know who you are such that succinct visits, remembers who you are and knows who we are. But more importantly, it's able to store data about a particular session. It's able to store your ID of the user and information about that user. Or in this case, It's able just to store all your reminders. So in order to take advantage of sessions, instead of having a global variable called reminders. And going to go ahead and delete this and install it. Sorry, instead, store reminders inside of the users sessions. So inside of the route and extra, I'm going to include a line like this. I'm going to check if reminders is not in not in requests that session. So so if reminders.'s Sorry, not a request. Not in so it's not an request that session. I'm going to go ahead and request a new session. So, meaning, if I go ahead and look inside of that session, and you can think of session as like a big machinery representing all the data that we have on, on file inside of this session about the user. And if reminders is not inside of that session, well, let me go ahead and add requests a session which is Reminders and set that equal to the empty list. So I'm going to, so I'm going to here request, so requested session. So request dot sessions. And reminders needs to be reminders are going to give it an empty list. So whenever a user enters that website and that user doesn't have reminders list inside of his for his website, web page. I'm going to create a new list forum because that is a different user. Let's say another user enters and he already has a list of reminders, this will be false and I would not recreate that list. And just to rephrase, what I've done here is that I'm looking inside of the session. I'm looking at set of decision to see if there are already a list of reminders inside of that session. And if there isn't, there isn't already a list of reminders and that session, well, I'd like to create that new session, new reminders list. So, so inside of square brackets, reminders equals to the empty list by setting that request session, if the user doesn't already have a list of reminders, go ahead and give them an empty list of reminders. And now he reminders. Instead of rendering that reminders, I'm going to render request that session. That reminder, sorry, square brackets, reminders. And you can see that that HTML, so sorry, index.html. I'm still looping over the same list of reminders because and that did not change. So I just and got that list using that requester session. Because now it's not a global variable, but it's a variable for that user only. And so now if I go ahead and save, used up by and go ahead and refresh my page. I'm going to get an operational URL. No such table, jangle slash underscore session, sorry. So this is a bit strange. This is a bit of a strange was going on here. No such table, Django session. Well, turns out as well. We're going to see that also in a while, Django tends to store data inside of tables. And we haven't yet gotten to what that ultimately means or how to manipulate or interact with data stored inside of tables. But Django stores data about sessions that would happen inside of a table by default. And you can change to have django store data about sessions elsewhere. But ultimately, Chienco hat as keeping data about who you are and who who you reminders are, and that there needs to be stored somewhere. And by default, django wants to store it inside of that table. And right now that table doesn't exist, so we need to create it. And the way to your Django access to that table that wants to create, that has been willing to create, but it hasn't yet, is to run a command or command prompt. And so that command is to run Python managed dot py. My great. And we'll learn more about what does that mean in terms of what migrations is and what is a mean to migrate data into a database. But for now, just know that Python managed up by migrate will allow us to create all of the default tables inside of Django is database. So now I click Enter. And you can see that I got these things. I'm going to see what does that mean, the error. And we're going to actually create databases of our own and tables of our own to store our own custom data. By Django has some initial before tables that a wants to create and running Python managed up by migrate allows for those tables to recreate it. So now that those exist, I first need to run my server. So Python managed up I run server and go ahead and copy that link. And so now actually I don't need to copy it, adjusts already. Have it. So I'm going to just refresh my page so it seems to be working fine. What now needs to change is when, when it comes to adding a new reminder, I'm going to actually also make some changes. Well, I'd rather then appending that reminder to my list of reminders. I'm going to add a request that sessions, sorry, request that session reminders equals two. Okay? And I'm going to add a plus sign as an Add to My Reminders list, list of reminders inside of the session that that new reminder. So. What we've changed here is we removed that dot append because now we don't have that list over here. We have that list inside of our session. So we're requesting from session that list, which is Reminders. And I'm adding to it that remind. So we just have to remove those because reminder is actually a variable, so you don't add those quotations. So now we go ahead and refresh my page. And the newer wonder, let's say add it. Submit. I have it. Let's say go to the gym. I have go to the gym. Now let's go and try to open it using a needle mode. So let's go ahead and refresh your page. So you can see now that we have no anew reminders. So this is not the same list that we've seen and on the other on website. So here we have no reminders and say I add coal, mom. And you can see that that list is different than the other. And we have a list of reminders that has come up. And here is our other list, which has it go to the gym. So now we've really just scratched the surface of what Django has to offer. But you see now the ability it has to be able to create dynamic web applications instead of just displaying HTML and CSS. That's on the same every time. Using Django, you've now had the ability to be able to generate programatically custom HTML and CSS, either saying hello to a person's name based on what the name is provided inside of that Euro. Or the ability to say, check the current date or display something. If the date is something conditionally and if the data sending not expected, and the ability to store data on a session basis, we are able to still store information about the users on, let's say reminders, list to do list, any other list, for example, such that the subsequent messages, they can see their lists of things they need to do with different lists for each of the possible users. And here is really just the beginning of where Django, Django has up. Or Jeff, you get really powerful when it comes to storing data inside of our databases and manipulating the data and interacting data in various different ways. And that's ultimately where a lot of our, of our framework like this ultimately comes in. And we're going to explore that. And then next upcoming lectures, we're seeing models. 19. SQL: So as we said in the previous video, Django gets powerful and our web applications get more interesting as we start to delve into the world of data and trying to have web applications that store data inside of database, of a database. And to do that, we're going to first introduce a couple of topics. And these topics are going to first include SQL. So SQL is a database language that we can use to interact with databases. And Django is going to allow us to have an abstraction layer on top SQL to interact not by writing direct SQL queries, but by interacting with Python classes and objects that we are going to refer to as models. And migrations are going to need a technique that are going to allow us to update our database and responds to changes that we need to our underlying models. So before we get into things that are more Django specific, let's begin with a discussion of SQL more generally and more broadly, just a discussion of data and data types that we were going to want to store. And now there are a number of ways that we could store a data outside of our computer system. But oftentimes in databases, in particular, in a type of database known as relational database, we're going to store data inside of a table, which in which each table is going to have rows and columns. So, so for the sake of today, we're going to imagine starting to construct a database by an application that an airline might use, for example, for keeping track of various different flights on that airline and keeping track of which passengers on those floods. And so what we have here is a simple table, for example, of how you might represent a whole bunch of flights related data. Here, I have three columns for three pieces of information and that I might want to keep track of for any particular flight. So for any given flight, I might want to know that that flight origin, what Sydney is leaving from its destination, what cities it's arriving at, and its duration. Some value in minutes of how long it's going to take that flight to get from the origin to the destination. So each of those columns represent one field that I might want to keep track of my data. And each row meanwhile, is going to represent an individual flight. So here, for example, is one row representing a flight from Beirut to Dubai. And that flight just happens to take 400, sorry, 220 minutes. And so what SQL is going to allow us to do is it's going to be a database of language that's designed to interact with relational database management systems. Databases that organize data data into tables where every table has rows and columns. And it turns out there are a number of different database management systems that implement part of the SQL standard of this type of language. Some of the most popular are must, MySQL, Postgres, SQL, and SQL light. But there are definitely others as well. And each of these have various different features and different use cases were some might be more appropriate. Mysql and pour both agree SQL, otherwise known as just both grieve. More heavier database management systems, so to speak, are generally running on servers elsewhere were a lot of big companies will use servers like these in order to store data and represent their data in a sort of separate database. And the idea of that is that you might want to have the application running on one server, but you might want your database running as an entire separate process such that they can behave independently. You can debug and test them independently. And if one goes down, the other doesn't go down as well. Sqlite as a bit of a simpler, lighter weight complementation of the SQL standard. And what SQL light is going to do is rather than be an entire server that's going to be listening for requests and making responses. Sql light is just going to store it's data as a single file. And many of the syntax that apply to SQL can also be applied to other languages as well. And other database management systems do. So. That's just an overview of what is SQL databases. However, you don't need to know this information. It's just a basic introduction to get us started to Django. So we're going to be using, and we are going to be seeing that in the next videos. How would we be using models, migrations inside of Django that are based on SQL lite and which are databases. So this is, as I said, just a brief introduction into SQL. And we're going to be talking about models next. 20. Models: So now that we've taken a look at SQL, let's turn our attention in particular to Django models, which are a way of presenting data inside of the Django application. Because we're, Django is really going to get powerful in designing our web applications, is the ability to present data in terms of these models. So we're going to go ahead and create a web application that's going to represent what an airline might want to store inside of its own application. So the first thing I'd like to do is create a Django project. And also I'll be going ahead and in my command prompt, this is the forward I'd like to create my project in. And we'll go ahead and start by writing Django Admin start project and followed by the name of our project. So we'll be calling our project airline. And creating a project for an airline's website, for example. And I'll go ahead and click on Enter to run my command. And now, if I go to that file to Django, I can see that now as i from having that project called Project one, I also have a new project called early. So let's go ahead and to, to our Visual Studio code. So here I'm going add and clicking on Add Folder to my workspace. And so this is my project that I liked. So here is managed R5 and here is setting stopped by and followed by other fives. And remember, before actually beginning to edit my code, every Django project needs to have one or more apps within it. So the first app that I'd like to create for this airline is one to keep track of floods. So keeping track of flight information like Origins, destinations, and durations, and what passengers are on these flights. So in order to create an application, I first need to enter that project. And so we go to airline, copy the path and then go to that path. And then now inside of that file, I'll be creating my application. So in order to create a new application. Python manage that by start up. And as I said, I'll be calling that app flats. So now I've ran that command. I can see that I have added an application into my project called floods. And so when I create an app, the first thing I need to do is go to settings out phi and psi of airline and add that newly added up into my install apps. So now my installed app is called flights. So the adding it. And so now flights is an installed app. Then what I'd like to do is say, go ahead and go to your OS PI. And which is again the table of contents for all the URLs that I can get. For this particular web application. I'm going to include imports or include because I want to, what I want to do is when someone visits that path of, let's say flights. So path when someone visits the path of I'm flights, I would like to include all the flights, dot URLs, adding dance. You are also pi phi that will be inside of my flights application. So first let's go ahead and go to floods dot URL. And I'm going to be including, so I'll be including all my URLs. So okay, and those mutations. And now, so That's actually yes, and let's close that parenthesis. And so now of course, I need URLs dot py file inside of my flies application. So let's go ahead and save that and go to floods. And I'll be creating a new file called URLs dot py file. So the first thing I need to do is from DOD import views and RAM, Django, dot URLs, import path. And then, and they were all patterns are going to be a list of paths. So, so let's go ahead and save that. And now we can begin. But before we begin with dealing with actual URLs, the first thing I would want to do is create the modals. Modals are going away of building a Python class that's going to represent data that I want Django to store as a database. So. When it creates a moral, Django is going to figure out the SQL syntax needed to use to a create table, but then be manipulated table whenever I make changes to those models. So here what I can do is inside of my application that gets created. So here flights is application that I've created. There is a.py file and we haven't looked at, actually looked at this file before. But this is going to be the place where we get to far defined what models are going to be looking like. And so, um, and so here are going to be existing. And so now every model is going to be a Python class. And you can think of this of having one model for each of the main tables that we care about, storing information about. And you can notice that models is already imported, so we don't have to import it. And now let's define our first-class called Flights. And it's going to inherit from models, dot model. So the model. And then I need to provide inside of this class all of the parameters that this flight has. So what properties does this flight hub that I might want to keep track of? Well, a flight has an origin. So again, begin by running, writing soil origin. And so origin is going to be a models dot char, char field. And and so this is documented on Django website in terms of the various different fields that exist. And that I can include inside of Django model. Where here I'm seeing, here is a character field whose, let's say max, maximum length is let's say 72. So I'm going to assume that this has, this origin, has a maximum length of 72. And so it's worth mentioning that a char field has to have an exon length as an attribute or an argument. We have to specify the maximum length of each. And so now every flight will also have a destination, which will be a character field also. So char stands for character. So destination is going to also be a models dot char field. And of the same maximum length, which is 72. And so now every flight is going to have a duration two, which is going to be only an integer field. So this is my way of telling Django to just add an integer field. So duration equal to models dot integer, so field, so integer field, because I'm going to be adding an integer to that field. So now this is, as I said, my first Django model is a class called Flights, where I've defined all of the properties that a flight has. So this is actually flight that floods because this class is going to represent a table of flight. So it's one flight object which has many rows, so many flights within the same row. So it's flight. And as I said, this flight, this flight class where I've defined all the parameters that I want. And using Django syntax, I defined all the things that they should have. They have an origin and destination and a duration. And so that's our first step into creating models. 21. Migrations: In this video, we'll be looking at migrations. So let's go and see the application that we've been working on. So this is the model that flight model that we've created that has three variables. The origin, destination, and duration. And the origin and destination are character fields with a maximum length of 72. And the duration is an integer field as you can only enter integers. But of course, nothing here is actually modifying my database that Django is using in order to store information about my application. So what I'd like to do is somehow tell Django that you should update the database to include information about the models that I have just created. This is a process that we refer to in Django and more generally as my regions. I'll create a migration to say, here are some changes that I would like to add to my database. Then my breed them in order to take those changes and actually implement them to the database. So it's a two step process. One is to create the migration, the instructions for how to actually go about manipulating the database. And then we need to take that migration step of taking those instructions and actually applying them to the underlying database. We can make the migrations. We are command so and my command prompt. We'll be writing manage dot py and followed by. So math.pi is, uh, has a number of different commands that we can use, which will control various parts of the application. And now we'll be using math.pi two, make migrations. So, so make migrations. And so if I click on enter. So now what I see is that no changes detected and that's because I did not save my file. So now let's go ahead and re-enter. So so adversely did not detect anything because I didn't actually add a new model before saving it. But now, after saving it, if I go ahead and type in math.pi, make migrations and click on Enter. We can see that we are given a migration for flights in one initial dot py. We're in this migration. We've created a model called flight. So this is the Migrations folder that has been created for me. I didn't have to create it myself. And that's the file that is created. And this file has instructions to Django for how to manipulate the database to reflect the changes I made to model. Here is an instruction to Django to create a new model called flight. And that has particular fields inside of it. So we have the origin and destination and the duration that we've created. And additionally, we have the id. And so the idea is created by default for any model. And that ID is specific to each flight. Let's say object. So let's say I have three flights. The first flight is going to have id of one. The second flood is going to have an ID of two, and the third is going to have an ID of three. And so ideas are useful for us to, when we start calling on objects. So let's say I want a specific flight. And so I can get that specific flood from its ID. And we'll be seeing more about that in upcoming videos. But for now, just know that every model is associated with an ID. Which might be a fueled If you want to say. So, which is created by default. So it's basing off of the changes that I've made tomato stop by. The modal that I added is now reflected in this migration. Now, if I want to apply this migration, actually applied to Genghis database, I can run inside of my command line. Python managed pi migrates, click on Enter. And so now that I have within this command, there are a bunch of default migrations that are applied as well. But notice that one of the migrations that gets applied is the one right here. So flights dot 01 issue. To say that let's go ahead and apply that migration Greta table. That's going to present floods. So now you can notice that if I go to my file, I have an addition to Ireland and flights and manage apart. I have db.json SQLite three, which is the database of my application of let's say the table which is going to store the flights. So it has all the data about my floods. And so now I can actually begin to manipulate data and interact with sort of models. And we are going to be seeing that in the next video. 22. Django's Shell: Using Django shell, we'll see how we can manipulate data and interact with models. So we'll begin interacting with models first with Django shell, and we'll see later on how we can interact with models in other ways. And I can use direct SQL syntax by opening up the database file and running commands. But Django, Django provides some nice abstraction layers on top of it so that I don't actually need to execute those commands myself. I can begin to work more generally with Python classes and variables and things that are views inside of the Python language. So I can enter Django shell where I can just run Python commands. And so I can enter giant Django shell by running the command Python managed up by cell. So then I can begin to write Python commands to get executed on this web application. So I'm going to type in Python managed up by Shell. And click on Enter. And as I read this command, I am an Python Shell. And so the first thing I'd like to do as from flights dot models. So flights is the name of my application. And I want to get from w1, the models are pi fi. I'm going to import that model which is called Flight. And I'm going to click on Enter. And now, as I said, I'm importing the flight class from the models flight that I've just created. Now what I want, I can do is that I can create a new flight. I can say something like f equal a flight, let's say whose origin is. So let's write first flight, which is the name of the model and whose origin is say New York. And let's give it also a destination. And let's say the destination is London. And the duration of this flight is going to be, let's say 415 minutes. So 415. And it can see that we did not 415 inside of a string because we specified that the duration is going to be an integer field. And now I can hit enter and then. Even after save, in order to add it to my database. If I don't click Save, then this flight won't be added to my database. And so as I said, the syntax is my way of inserting data to the table. And now I just have to add a Python command. And Django knows that why create a new flight and save it, that it should run in sentiment on the underlying SQL deals. Where here I've created a new flight with this particular origin and destination and erosion. And I'm going to ahead and see that flight as well. If I want to compare that Floyd, maybe get information from their flight. I can say something like flight that objects. That o, for example. And that is going to give me is equivalent to saying first, select all in SQL as in give me all the flights that are inside of my system or my database. And so you can see here that present Shell gave me an error saying that light is not defined. And this is actually because we added flood with lower case another hierarchies. And so it has a, it has to be an uppercase. So let's fix it. Supplied dot objects, dot all. And now let's go ahead and click Enter. And you can see that we are returned with with acquiree set that has a flight object and which has and so this is, sorry, this is the flight class and this is Flight object one. And we have one fly that came back from flight object one. So a flight have been created for me with the ID one. So one stands for the ID that we've talked about in the previous video that is created by default. Now Flight object one, Probably not at all helpful, but name it be nicer if this model had a cleaner way of seeing the name of a particular flood, for example. And it turns out that we can do that any model. I'll go back to the code inside of Modesto pi, which is right here. And any model can implement double underscore STR function. So I'm going to begin by writing that function. And so is the argument. And so self is the flight itself, the object file object itself. And so this is a string representation for that flood object that doesn't apply to Django objects only. But Python classes generally that this Python classes going function, sorry, is going to return as string object of that class whenever we try to print. And so here will be returning a formatted string, which is going to be including self, that is so that object dot idea. And let's say followed by. So that origin, so ID then origin. And origin of course is a destination. So several origin to self.position. And this is a formatted string, so everything is going to be inside of curly braces in order to replace that variable inside of our string. And so that's about it. And so here what I've said is that the string representation of any flight is going to be a string that gives its ID and then says, or gender destinations. Just a nice clean name that is going to represent this particular flight objects. And so just to, for you who don't know what is a formatted string. So whenever you run, wants to place a variable inside of a string, you first begin by adding f. That's the stats for Formatted. And followed by the variables inside of my string, just inside of curly brackets. So here for example, so that id is a variable where I've put it inside of curly braces. And of course, the string is, begins by as a formula. So now if I go back to my Python shell and let say and type and exit to rerun our shell. So yes, and we enter Python managed up by. So. Now I first need to, again say from flights dot poor showing models. Models. I'm going to importing flight, which is our flight class. And so enter. And now what I'd like to say is, let's let a variable called flight to be equal to flights dot objects at all. So flights equals to fly that objects dot o. And now I'd like to visualize that flies variable. So I'm going to type in flights and hit Enter. And so now what I see is that this is a much nicer way of representing the string representation of the name, which makes it a little bit easier to interact with it. So we have Esquire set, which has one flight with id1 from New York to London. As I said, now it's much easier. It makes it a little bit nicer to interact with with it. And if I just wanted to get this one flight, I can say something like flights dot on first. So flights equals to light. So flights that first. And so now. So let's just remove us. So this is a new variable, just to see it. So now I create a new variable which is floods dot first. And so let's go ahead and see that variable. And so flight is acquiree, sorry, flights is acquire set, which has one flight. However, this is one flight object. So one object, not a set of flights with ID1. I'm from New York to London. And so now I have this flight. And as I said, from New York to London as an any Python object, I can begin to access these properties of that object by saying, let's say flight. What is our ID? Or flight was your origin or what is your destination? And endline? What is your duration? I can access all these variables of this flight. There are ultimately care about by just typing symbol code. So let's say, and I want to get ideas. So I just type in flight, that idea. And so the idea is one flight, the duration. It's 415, flight on dot origin. It's New York. And that's inside of a string. Flight dot destination and London. And I can say something like delete that flight. So I can just write in flight that delete. Sorry. And so now I've deleted this flight. So if I go ahead and re-enter that flight, squire set that I've had. You can see that now microwave set is empty because originally it only had that flight that I just do it. So now this flights are able core set is empty. So this is how we can create, let's say, module on or model objects inside of poison show, or let's say Django shell. 23. Foreign Keys: As we reach this part of the course, we're going to see how we can use foreign keys inside of our models. So this is the flight model that we created that has three variables and origin to a flight and destination and the duration of that flight. Ultimately though, this is not the model that we actually want to present in my flight. Because here again, I'm using accounting field for things like the origin destination. When in reality, I probably want to use something like another table for representing airports and then some relationship between every flight and airport. So let's go ahead and try implementing this idea right now that I can go back and create a new class. Aside from my classified. And this class is going to be an airport class. So Airport and the same as flight, this airport class and going to implement the models. So from modeled our models. And as here, okay? So as I said, this is also a model. And I would like for this airport class to have a coat, which is a character field. So models dot caret character field. So char field. And I'm going to give it a maximum length, which is unnecessary argument to any char field. So let's say the code is a maximum of three because it can consist only of three characters and as well as a C. So I want to see inside of my airport. So sorry, the airport dentist and salad SAT. So it's going to be a model shot character field two. And let's give it a maximum length of 72. So, so I'm assuming that the maximum length of my city is going to be 72 or less. So now we've written, and so we've written the code and the City, which is under this enter. And now, let's go ahead and give this airport model a string representation, the same way we did for our flight model. So we're going to add that function, STR. Function. And I'm going to give it an argument of self, which is on that report object, as I said, with the code city, Georgia variables. So specific code and a specific city. And so that's the generous, a formatted string which contains the code. And let's first the city, and then followed by, let's say, the code which would be inside of a parenthesis. And so self.name code, close that curly braces and then curly parenthesis. And so this is how we will be representing an airport. And now our flight model needs to change a little bit. No longer. Will. Object to the origin and destination B char fields. They're just storing texts. But instead on, we're going to be using what's called as a foreign key. And so I'm going to replace char field and import origin. This sorry, arginines nation by this foreign key. And so this foreign key is going to reference that airport table. And so let's go ahead and write n here, foreign. So foreign key. And inside of that foreign key, I'm going to specify from where I'm going to add that reference, that of that other people table. And so I'm going to be writing Airport, which is retrieval that I'd like to refer to the reference. And so I can provide some additional arguments. So this alone would be enough, but I can add some additional arguments, like on delete. And I would like to models dot cascade. And so OK. And so what does this mean? Well, when I have tables that are related to each other, see, SQL needs some way to know what should happen if you delete something. If I have a flight from, let's see, JFK to New York, tour sari to London, Mimi. And later in time, I decide to delete that airport from my database. What should happen to that file? What happens to flight soaring up file is that it's referencing would be also the leading deleted. So all the flight of the airport would be deleted. And so if I ever delete an airport of that table, is going to delete all other corresponding flights of that airport. And there are other on delete parameters that we can set for saying like, don't even let me delete an airport if there are floods are going or coming from that airport. And that's called the models that protect. But there are other ways to implement similar types of constraints. And another argument that we could add is the related name argument. And a related name, as we'll see in just a moment, is going to be a way of me accessing a relationship in real, the reverse order that from a flight, I can take a flight and say dot origin together flies origin and airport. What the other question that I might also ask is its origin. If I have an airport, how do I get all of the flights that have this urban art as an origin? And so here, if I give it a related name to this foreign key, Django is going to arrive, automatically set up a relationship that goes in the opposite direction. And so here, if you have an airport and I want to know all of the flights that have that airport as the origin. The reasonable name for related name would be something like ARM departures. So I want all the flights that inside of an airport that are departing from the airport. And so we're going to be adding this, the same thing for the destination. And so before we add it, as I said, if I give a related name to this foreign key, Django will automatically set that relationship going in the opposite direction. And so here, if you have an airport and I want to know all of the floods that are, have this Airport as the origin. I am going to type in that airport with the associated, sorry would departures. And we're going to be seeing that and decimal. And likewise, when we were doing the same thing for destination, instead of a character field, I'll be adding also a foreign key. And on delete, I want to cascade and the related name. I'll be changing the relatedly. And so if the related name for the origin is the purchase, the most suitable name for the name for destination would be arrivals. And so because we said arrivals, because if you have an airport that I want my want to access all of the arrivals of the correspond to the airport. So I would like to add that arrivals. And so now I've added two things. I've added a new class called Airport, and I've modified my existing flight model. So this has changed my bicycle, but hasn't yet changed in my database. So in order to make that change in the database, as we said in the previous video. It's a two-step process. Step one, go to Mantua and go to that command inside of Python, manage soil. So inside of that command and type in Python mentioned but make migrations. And now to say look for NHE, new changes that have been made. Two models, dot-dot-dot. And go ahead and create a migration to make those changes to the database. And so let's just check if everything we wrote is correct. So here we have the the airport model, we have the code and the City, which our turf fields of maxlength 372. And this is how it's represented. The string representation. So origin is modest dot foreign key with this, these three arguments. And here as models that foreign key airport on delete and relatedly. So. And the same as for the origin and the related name. They will be on, sorry, the delete and the name which are the arguments that we've explained. And now we're going to click on run. And so now what I've created is a new migration file. And this migration file, which is right here, is going to tell me how on, sorry, it's going to create a model called airport. And this is also going to alter. So it's going to, as it's written here, is going to alternate distinction that we've created and alter the origin fuel in my flight because, sorry, origin and destination because we've changed those to be no longer be character fields, but to be. So as you can see here, we've change them to be instead of char fields or character fields, but to be foreign key. So they're referencing and particular Airport. So that something that has to be change in the database. And to make that change, we have to run something like Python, manage dot py, migrate, which is the second step. So if made those migrations, we've made those changes and through migrate and going to be applying those changes to my database. So now we've now applied this migration that you've just created and our database is now up-to-date. So what can we do now? Now, I can go ahead and go back to the shell. So Python managed up by trial. And the first thing we need to do on as soon as the shell comes is to import from the models that abort everything actually using the star. So, so first forum on flight on DOT models. I'm going to be important. Star, which is everything. So let's import. So first from the, from flights are models import. I'll be importing star everything. And so that's because instead of importing flight and airport. And you might have many other models inside of our models dot py. So instead of just writing each one at a time, I could just add that star, which will import for me everything inside of my modus dot py. And I can now create. So we've got now our first arrow which says module Not Found error. So let's just try and retype flights dot model instead of flight, import everything. So star. And now we have another error, which is the module not found error also. So the first time, the first error was no module found flight. Named sorry flight. So the knee of the modulus wrong. So it did not read it correctly. And that's why we've got the first era. And now, although I've have fixed that module name, models is not Corinthian correctly. And thus why we have that module Not Found error. So to fix that modules. So if flights that models and of course preceded by Forum. So from flights are models. I'm going to import star. Now. I can create an airport. I can say something like JFK equals to the airport, which is the name of my model. And this airport has to rivals a code of maximum length of three, which happens to be JFK. And at C, let's say is New York. So say as New York. And of course instead of character strings because this, these are character fields. And now of course, save. We need to save them because if we don't save them, we're not going to be able to add it to our database. Enter. I can now create another 12. So alpha equals to airport city. Sorry, first code, which as they employed, Stella char, and C is going to be one. And of course, inside of character strings. Close the parentheses and hit Enter. And of course, don't forget to save, which is the most important step. So I can also do one more just to visualize how our table is going to be. So we have, you can add dx. And so this is an airport which has which has the code Dx. And the city is Dubai. Cuz depredations. So Debye and save that of course, the most important step, dx dot c. So now that I've created the airports, JFK, Elektra, and dx beam. And so now let's just, let's just try adding one, last one because we are using these airports to create several flights. So just in order to have a variety of reports and floods. So learn a report which has a code I'll earn. And comma city. And the city is me le Nations. So okay, and now the one most important step we need to have is to save the report so I end up unsafe. So now I've created for efforts that can be added to my upper database. And now I can add flight. So I can add if light, which is being called to flight object, the origin, Let's say is char. Oh, Jeff gain as manner. And the destination is adage or the, sorry, the duration is equal to 415. And I'm going to go ahead and save that floods. So I've created the four airports. I've created a flight and seeded if I type in flight now. So the name of the flood or agreed. And now I can see that this flight is from New York to London. But I can also notice that the code is JFK and other chore. Because in the representation, the string representation, if my, of my airport, sorry, I specified that I wanted the city and followed by the code of that city and set of parenthesis. And so this is my first airport, and this is my second airport. This is the destination, sorry, the origin. And this is the destination. And this of course is the ID. But I can also now say, not just by adding a flight. Aside from having a flight, I can say flight dot origin and fight an origin is going to give me a report. So that airport is JFK. Jfk, and the city is New York. I can also say, I say I don't want the code, I just want to city. So flight dot origin, dot city. And that's going to give me only New York. Let's say I want the code fly dot origin, dot code. And that's going to give me JFK. And if I start with the origin, something like JFK or lambda, I can say alert shark arrivals. All. So you can see here that we have on one side, we have acquire set that has one flight from New York to London. So all the arrivals that I going to London, Allah char, is only actually one flight that is from New York. And so this now gives us the ability to manipulate SQL just by using these python models. And I now have Python classes that represent all these variables, all these different types of data. I can interact with classes and their properties. And Django is going to take care of the process for me in order to and tell what the SQL queries shouldn't be to perform these saves and adding those airports and flies to my databases. And so in this video, we've seen how we can make use of foreign keys to relate to two different tables to each other. By so we're related that airport, that flight. And we saw how we can manipulate them and get some data out of them using the Python shell or jenkins shell. And we'll see, we'll be explaining more and more of these in the next video. 24. Viewing Models: In the previous video, we have seen how we could create models, how we can create tables until the Django databases. And now we're going to see how we can view these models are webpage. So these are our two models, the airport and the flight, which as we said, are related to each other. So the flight have a foreign key or the airport. And so the same is for origin and destination. And now we can begin to design and what application around this idea that again and go TRL. So pi of that flights application and add a path to my URL patterns. And let's have a default path that would, would display for us a list of all the flights. And so we need to have a function. And in order to do that, we're going to go to views that index. So we recreating that function, that index function in a minute. And let's give it a name of index and save it. And now we're going to views dot py and create that function, that index function, which is going to get a request as we always do in views dot py. And what I would like to do is just display a list of all the floods. So I might meet from DOT models too, import that flight and that airport models. So I'm going to be pouring flight and put ok. And I just want a list of all the floods as I said. So now what I'm going to be doing is returning a rendered HTML page. So render upon request. And so let's render a page called Flights slash index.html. And let's give this HTML page access to a variable called Flights. And what is that variable is going to be equal to? It's going to be equal to flight. So flight is the name of our model that objects dot o. To get me all of the flights that I would like to put right here. And so now what I need to do is actually create those individual templates. So inside of lights, I'm going to be creating a new folder called templates. Inside of which I'm going to create another folder called flights. And I'm going to be creating first the layout HTML page. So that's the basic one which will be inheriting from. And so this page is going to contain the basic structure of our webpage. So I'm going to add a block here. So I can just add it inside of the other HTML page. So that's this section where I'll be adding those flights. And let's go ahead and give it, let's say a heading of flats. So this is the default page 30, this is the default layout for my page. And now let's go ahead and create index dot HTML, which is going to extend that layout page. Layout that HTML, which is inside of my flights folder. And now inside of the block body. I'm going to somehow add these floods. So in order to do that, let's first create an unordered list. And inside of that unordered list, I'm going to be looping over that flights variable that we added. So for every flight inside of our flights Bible, I'll be. And so that flies efforts is the one right here. So that's the variable flights, which is a list of all the flights objects that we have. And now we're going to loop over each flight inside of those flights list that I acquired by running flighted adjuncts at all. And this is my way of using Django API. And the things that Django has given me access to to get the flight that are stored in Django is database. So let's just type in here that every time I go over flat, I'm going to be creating a list item. So looping over these flights and printing the flight in each loop. So inside of understanding, as I said. And so let's first add, let's say it's id. So flight number less a1, a2, an altar to add ID. Need inside of those curly braces to add flight. That variable flight that I created, which is an item of that list. And we have seen earlier that we've got that we can access properties of those objects, goes more logics that are inside of my database. And so I can do that. And also ingenuous HTML. So I'm going to access that ID by typing in flight ID. So Flight Number one, let's see. So that's the ID. And I'd like to see where it's headed towards going from where it's headed and where it's going. So flight that origin two and going to add to fly dot destination. And so, and that way, it would be clear. And I've put all the information I need to know about that flight. And it's ID, its origin is niches duration. So we can add, we can edit duration. So let's say instead of parenthesis, I deafly durations of light, dark duration, and a column, or Lesley without a column injustice. In order for them to understand that these are enormous. So that's going to happen for each of my flats. So now I'm going to be saving that and going to views dot py and saving it. And now let's go ahead and open the command line and run. Python, managed that.py, run server and take on engine. And as soon as the treasures that's just copy the link and open it in our web browser. So let's go ahead and paste it here. And I can see that pages not found because we haven't entered the application. So let's go to flights to the flights application. And so no, you can see that I have exactly one flight. And this is its ID one from New York to London, and its duration is 415 minutes. It's taking data from my database and now displaying it inside of this template. And if I were to add these flights, it would also update on this page as well. So you can go ahead and visualize that by adding and if light using Python Shell. And so go ahead and stop running our server and type in Python managed up by And followed by Shell in order to access our shell. And let's say that I would like to add a flight from Dubai to Paris, for example. Well, how do I get the input for Dubai and purse? And so it turns out, if I want to get to by, I can say two by, well first of all, we import flights from flies on models, import star. And then let's create our variable Dubai, which is the airport I would like to get. And so first, if I first normal to visualize the airport that I have, let's see. We need to type in first Airport objects that all. And the airport is capitalised. And if you go ahead and and to that, we returned with acquire set with all the reports that I have. So I have New York to buy an alarm. But I just want to get one of these airports, one for this nation and one for George. And so another thing I can do is filter my airports list just to not to get all the airports, but just some of them. So I can say something like airport dot objects, dot filter, and not all. And I'm going to be filtering those airport objects according to some category. So maybe I want my airports to be Forum, the city, New York. So you, your and if I go ahead and you can see, we're returned with this choir set, which contains only that airport domain, these. So we got that report which has Citi New York. And that's going to, as I said, give me only a list, one element of that choirs. And so that folder lets me constraint the results that come back and give me all their ports, but only give me airports whose city is New York, for example. And it's only giving me back one. So I could say dot filter city, New York or let's say the code. I could just add the code if I don't want to add the city. And as amplifier of doing the same thing, if you know, you're only getting one object. So one result back. I can save something like report dot. Objects, dot get, and city is New York. And now if I hit Enter, you can see that I don't have acquires that anymore, but I have only an object which is on that report, which is from that's actually from the cold JFK, New York. Jfk. But it would to throw me an error if there were more than one object. Which because as I said to get only gives us one. Let's go ahead and save that JFK airport inside of a variable in order to use it. So airport dot objects that get. And let's add the code that's shorter. So JFK and we are returned an error because the string wasn't completed. Sources go ahead and fix that problem. Airport that objects that get code equals two JFK. Because that an enter and so forth that copy that. So JFK. Okay, so now everything seems to be good. And of course I want to save that. So I would say that the by one. So Airport that objects dot get cote, which is t XP. And, and now I saved my airports inside of these variables. And now I'm going to add my new flight. So in order to add that flight, I'm going to go ahead and draw it. I won't create that variable flight equals to flight object origin, let's say is the Bly. So the X mean destination as JFK. And let's say the curation is equals to 500 minutes. So now I hit enter and I go ahead and save my floods and older to save it to my database. So now I've saved that flight. And now my database has two flats. One flight that's going from New York to London, and one flight that's going from Dubai to New York. And so now if I go ahead and refresh my page, so let's go ahead and refresh. First of all, we need to exit at Shell and rerun our server. So Python mentioned pirate server and open over page. Refresh it. You can see that we have that newly added flight to our webpage. So that's the flight we already had. And then you flight that we've added using Python Shell. 25. Django Admin: So in the previous video, you have seen how we could add flights to our database by using the Python shell. However, it's going to be pretty annoying if every time I want to update that data, adding new data or manipulating the data, I need to go into the shell in order to run direct commands that are able to add new flights and new airports and other models that you might want to add to our application. But what I'd really like to be able to do is just very simply. And these new flights, these new airports via a web interface, via the web, and we'll be able to say, all right, let me do, let me add a new flight that goes from location one to location two. And it's possible that using the information that we know now to build a web page, that just, that does justice. But Django is built on this idea that it doesn't want to the programmer to have to repeat that, repeat that work that other people have already done. And this process of trying to find different models and very quickly be able to create an edit and manipulate models is so common that jangle has already built for us an entire app that is just designed for the manipulation of these models. And it's known as the Django admin app. And this is an app that we've seen. Traces buffer already, if you remember, in your L's are pi of our application. So we can see here that, oh, we added a path of our own app, the flights up. But there was already a path given to us by default, the slash M, And that takes us to the Admin App as well. And so in order to use the Admin App, we need to create an administrative account inside of our Django web application. And the way to do that is via the command line. I can run inside of my command. Python managed dot py and create super user. And so that is going to make Django asked me to write my username. So I'm going to go ahead and type in Sarah. And let's say my email is Sarah, Hotmail.com. And let's say the password is whatever I wrote. And you can see that if you write at two common password is going to give you that. And especially if it's less than eight characters, is going to give you like a security comment alert that you need to have stronger password. So. In this, we can create the user anyway with the password that we added and we can recreate it with a stronger restaurant. So I'm just going to type in a stronger password. So now the superuser is created and creates successfully. So Django has created a superuser account for me in this web application so that I, using this credentials, have the ability to visit the web interface for the Admin App and actually manipulate some of these underlying models. So in order to do this, the first thing I need to do is take my models and add those models to the Admin App. And so inside my modals, dot py, which is on the flights. If I look at my modal stop by, I have a class called Airport and a class called flight. And if we look at these files right here, you can see that we still have some funds that we haven't discussed yet. And one of these is the admin dot pi r first from my models that pie inside of my map admin page or file. So from models. So models, I'm going to be importing those three facets, which are flight and an airport. Sorry, two classes, flight and airport. And so now I am going to say is that admin that side, that register a report. So in order to view that inside of our model's going to be doing the same thing for my flight glass. And just to copy that and change airport to flood. And so what this is going to do is it's going to tell Django Admin App that I would like to use the admin app to be able to manipulate airports and to be able to manipulate flights as well. So let's take a look at this admin app and see how it actually works. And in order to do so, I'm going to open my command prompt and type in Python, mans dot py and run server. And I'm going to copy that link and don't to my web browser and just type it here. And so now in order to enter that admin app, I'm going to, instead of going to stash flights, which is going to take us to that flights up or flights page. I'm going to go to the path that is specific to the antenna, which is admin. So slash Admin. And so you can see here that this general administration owl is not written by me. Django has written this. And it's asking me to log in. And I'll go ahead and login using the credentials that I just created a moment ago, typing in my username, which Sarah, and the password. And I'm going to go ahead and login. And what I get here is Django site administration interface, both for me but built for me, sorry, by Django, where I didn't need to design this at all. But importantly, if you go down here, I know I have the ability to add and manipulate airports and flights via this web interface, this Django admin interface. So now using this interface, I have the ability to manipulate the underlying database. To not only, sorry, to, to manipulate my models, to add and modify data that already exists. We see here all the airports that I've added to my database, Milan, Dubai, London, and New York. And so I can add, you want, I can say go ahead and add new airport. And let's say I want to add Abu Dhabi. And I'm just going to click on save and add another, let's say I want to, another one, let's say Istanbul. And so I've just save. And now you can see that I've added all these airports without having to go to dangles, shell or Python. So Django was originally created for news organization that if you wanted to be able to post articles and posting you both on a website. So it was made very easy Vn interface to just say, here is the new article and here's the content of the article to be able to display on a page. And now you've been able to do the same thing to our application. I've been able to very quickly add new airports to our website as well. So if I want to add flights now, I can go back home and click on flights. And now I can see that I have already two flights inside of my database. I have one from Dubai to your ad, one from New York to London. All. I can also add a new one by clicking Add flight. And it's setting. We choose the origin and choose the destination. And I have a duration right here. And Django knows that the origin must be an airport. So it's going to give me the opportunity to choose from one of these airports where I can say, okay, go ahead and set a flight from Abu Dhabi to, let's say Milan. And let's say that the duration is 200 minutes. And so now if I go ahead and save and add another that say I want to add another one from Assemble to, let's say London. And the duration is 400 minutes. And I'm going to be saving that. You can see that now I have four flights. I've been able to add a number of different flights and a number of different airports. And if I go now back not to my add-on app, but my flights up, mug owing and typing in in my URL, flight, sorry, flights. I can see that now on my webpage. I have all these newly added flights along with the ones that were already founder before I added those two new floods. And so now you can see that via admins, Django, and an interface, I've been able to add these flights and visualize them on my webpage. And so now what I might like to do is begin to add some more pages to this web application. And we'll be seeing how to do that in the next video. 26. MultipleWebpages: So now continuing on where we stopped in the previous video, where we were able to add new information to our database using the amine up. Now, what I'd like to begin to add is to add some more web pages to this web application. Meaning that this application is going to be more sophisticated by maybe giving it the ability to click on flight maybe, and if you information about that flight. So what I'd like for this to show me, that is the details of that flight on on a page and zone. So let's say I go to here, Flight slush one. I like to know the information about flight one, which is from New York to London's and so on and so forth. So I can go to flight to, and it will give me the information about flight to. And now what I can do in order to do that is go back to URLs dot py and create a new path. So inside of my flights application, I'm going to open your alza pi and I'm going to add a new path to my application. And as we said, that each model has or each object model has a specific ID. And so we can refer to these flights, these flight 1234 by making use of the ID. So this is the idea of flight one, this is the idea of light two and so on. So what we'd like to add here as new parameter is the ID. And so in order to do that, we're going to be adding an integer variable. And same way we've added a string variable previously. Now we're going to be adding an integer variable. Let's call it Flight ID slash silica. And now, of course let's close that. And this is going to take us to the views that let's say flight function that will be creating in a second. And let's give it a name of flight. So, and of course, I'll add a comma to separate patterns. That's and save that. So now I've created a new path to my application. So this due path is any integer. And integer I type L is going to be using an hour flight functions to get the ID. So now if I go to use a pie and create that flat function, so fly. And so this flood function is going to take as a parameter the request and of course, the ID that we entered, a URL. And so let's give it a name of idea. So. Sorry, FlightID and has the same name that we used, an empath. And so now what this flag function's going to do? Well, the first thing I need to do is actually get that flight. I can say something like flight equal to the flight model dot objects that get, which is going to get only one object. And that object has the ID, flight ID. And I can use a more general generic variable called PKA. And so pk is a shortcut for primary key. And so it's a much more generic way of referencing the primary key for wherever the primary key happens to be called. A primary key in this case is called ID. And so now we've got this flight at, we want to get information about. And so now what I'm going to do is return, render upon request, an HTML page. And let's say that this eggshell page is called Flight slash flight dot HTML. And I'm going to pass as an argument to this stable this flight object. So in order to do that, let's just remove that. And I'm going to type in flood. And this slide is going to be that flight variable. So I'm going to save that. And now what you need to do is go to templates and create that flight dot HTML. So create file flight layout.html. And of course, go to the index.html is flight. So it's good. It's going to extend it or inherit everything. We have another layout.html. And now all we have to do is just add that body block. So log and their name is body. And so now inside of that body, what we'd like to add is something like, so first we're going to be getting that flight idea. So I'm going to type in, let's say, and heading to flight. And then inside of w Curie places fly dot id. And so that fly criteria is the parameter that we gave to this HTML page inside of USA, which is right here, which is the flight object regard using that method. And now, after I specify that flight ID, let's specify the estimation that distinction, origin and duration. So we can add an unordered list and list item one, which is the origin of that flight. And so in order to do that, origin is flight dot origin. And now we add another list item, which is the duration. So firstly destination and lastly the duration. So this lesion is flight dot desk Tunisian. And you can notice that these are commands that the, that you use inside of our Python shell. And we can also use them here inside of our as shameful Django language. So the methods that are used on that model objects can be used here. Supply, duration, and close that. And let's go ahead and save that. And now if I go ahead and load not flights, but let's see flights one. You can see that now are littered with them information about flight number one and flight, let's say two gets me, of course, information about flight T2. And particularly for that flood, it's printing its origins, duration and station. And so what I can also do, keep that flights only for the index and not for the layout. And we can do that by just going to layout and remove that flights. And just added to my index and side of the body, of course. And let's go ahead and save that and refresh that. And sooner it's much prettier, much more organized. So now if I go ahead just a flight, I can see that flights is specific to the index page, tsunami in pH. So let's say I go to four. This will give me information about flight number four, which is here. Now, there is some error checking that we probably should do here. Let's try to access a number of a flight that does not exist. So C seven, I'm going to get some sort of error that this does not exist. I might like to control what happens to that situation until the better. So you might imagine adding some additional error checking to handle those cases as well. But we leave it at this for now at the moment. So you can see how added pages to this application and made use of those functions that flight at origin, light, dot, destination, and all those variables that are soluble model right here in our show page in order to get information about that object. And so what I'm trying to say here is using those functions, flight dot objects that get, we can get our model objects and put them inside of our HTML pages and make use of these information. We can just easily add them to our web pages. 27. Many To Many Relationships: In this part of the course, we will be seeing how we could have many to many relationships between two tables. And so you'll be seeing that using our previous example and flights. And so till now, we've had the ability to have flights and airport models or tables. And now let's go ahead and add aside from these flights and airports to have passengers to add to our flights as well. And to be able to present these passengers. There might actually be on these flights to so we can add all the passengers, the ones that are on, are on a flight and those that are not on those plots. And so these passengers, this passengers model or TDL, is going to have a many-to-many relationship with our flights. And we're going to see that and explain why. So inside of my models, dapper, in addition to an airport and a flight class, I'm going to create a class called passengers. So plus passenger. And of course it's going to, I'm going to make it a model. So models dot model. And, and so this passenger, of course, is going to have a firstname. So first name. And that firstName is going to be a model of course. So Chore Field, character field. And of course, half is capital. And let's give it a maximum length of, let's say six. So 60 is the same for our last name. So models dot char fuel with a maximum length. And that maxlength is 60. And so passengers also, as with scribe, must have a many-to-many relationship with flights. That a flood could have multiple passengers. A passenger could be a multiple flights. And ultimately, we need an additional table to keep track of this. But we can think a little bit more abstractly here in Django and just see that every passenger has flights associated with them, which are a passenger flights. So of every flights have passengers inside of it. So in order to describe the relation, I'm going to type in models that many to many relationship. And so in order to use this relationship and going to By adding a many-to-many field. And so this relationship is going to be between my passenger class and my flights. So I'm BY adding as an argument of flight, which I want this class to be in relation with. And let's give it a blank equal true, meaning that it's not going to give me an error if a particular passenger doesn't have a flight. And we also give this a related name. So related name, which is passengers. So let's just fix this. So capital t. And so now we gave it a related name of passengers, meaning that if I have a passenger, I can use that flight. Attribute access all their flights. So all of the flights of the passenger, I'm going to access them using that flies variable. And likewise, if I have a flight, I can use that passengers related name to know to access all the passengers that are on that flight. And we'll see how that will be useful in a moment. Now, Let's add a string representation to this class also. So this string representation is begins by writing the definition for by CR and then self, which is the passenger object. And let's return a string, a formatted string, of course, with the passengers first_name and last_name. So self dot first name and then search space so that lastname. And close that, which is a reasonable way to represent a particular person. And now I need to apply these changes. And remember, in order to apply these changes, we need to have two steps because we did something to our models. So we need to update the database. So what we do is and before we add that, we can see that we have an attribute error. So module has no attributes many-to-many field, and this has to be a capital T. And let's go ahead and save that and now we can continue. So you can see that the error has been removed. Now, in order to update our database, as I said, we have to have two steps. The first is Python, managed dot py, run server as I make migrations. So go ahead and see detect these changes. So I created a new model. And now let's apply this updates to our database. So Python managed by migrates as an applied these changes that I've just made. So. And you can see that everything was successful. And I've applied those to an actual database. And if I go now to admin dot pi, let's go ahead. And aside from having an airport and a flight that suggested also a passenger. So I, of course I need to import it from models and added to that argument offer gesture. So passenger, go ahead and save that. Now, this what I just did here made me via the admin interface to manipulate passengers as well. I can now go ahead and run my server and type in Python, managed up.py. Run server. And click on Enter. And I'll go to my page. And now let's not go to sludge slides, but slash outmanned. And I've already logged in, so I don't need to add my credentials again. And now you can see here that I have an additional row inside of flights which places any of my application. So I'm going to go and passengers. So let's say I am, I'm adding a first-name which is Tobias. And lastName, she's eaten. And let's go ahead and save and add another. And you can see that Django did not give me an error because I didn't specify which flight these passages are on. Because I said we added an attribute which is blank equals to true, which makes it acceptable to leave it blank. So let's add tourists prior to save another Let's say Caleb, prior and safe. So you can see now that I have these passengers that all exist in Django admin interface. And now what I'd like to do is on my flight page, display information about which passenger happens to be on any given flight. And so before that, let us just adds up passengers to some flights. So let's say Tobias Eden is on flight to I'm just going to add the save. And now let's just go to passengers, see twice a year. And you can see that he has one flight from Dubai to New York. Now, let's see how we can add those information to our webpage. So he wanna go to flights. And in order to do so, I'm going to go to views are pi first inside of user by aside from giving access to that flight object, I'm going to have access to all the passengers that are on that flight. And to do so. I'm going to create a variable. Let's go passengers. And I'm going to type in flight, the passengers dot o. And the reason we can do this is because passengers right here is the related name, which is here. That here. So using that related name, recant access. Access all the passengers that are on a specific flight. And so now I'm going to save that. And inside of my flooded as Shamil, I'm going to add some additional information, which is let's first write a heading, type and passengers. And what I'm going to do is let's see, create a loop. So a for loop. And so for every passenger inside of my passengers variable that I've just passed in to that flight, that HTML. So just passengers. She's right here, which is a list of passengers. I'm going to go ahead. And so let's go ahead first and create an unordered list. Inside of that unordered list, I'll be looping over every passenger and passengers. So and for every passenger inside of my passengers list, I'm going to put that passenger inside of a list item. So passenger. And the moment I write that, what's going to be printed here is that string representation of passenger, which is here. So it's going to print for me his first and his last name. So let's go ahead and let's say that if I don't have any passengers, so if empty, let's just go ahead and type it. So if empty, I'm going to add, let's say a list item that has no passengers. So inside here, I'll be critical list-item, no passengers. And let's go ahead and save that. So now, if we go back to my page and let's go to slash one, I can see that now Flight one has this information about that flight and along with the passengers. And you can see that this flood till now has no passengers. And so the amino passengers. So let's go ahead and check into and you can see that two bys Eden. Is a passenger on flight to, and you can see that it's going and knowing that I have to do everything using that URL to go back and forth between pages. I can link these pages if I want to. And the way I do that is let's on a flight page. So on that flight or the channel, I'm going to add a link that links me to my main page. And I can add a URL. And this URL is called Index. So it's going to get me back to the index page. And let's call it back to flights. And that will get me to that index.html. And likewise, I'd like to, whenever I click on the flights, I'd like to go to see the information about that flight and an order to do that. I'm going to add a link to each of these flights. And that's going to be here. And so this is going to take me to the URL, which is flight. And let's just go ahead and write that URL for flight. And you can notice that inside of our flight path, which is an URLs dot py, which is right here. You can see that that path takes as an argument identity. And so here, inside of index, whenever I want to call that URL, I want Django or know what flight, which flight Dewey we mean by. So a flight of ID 123. And so we have to specify what id. And in order to do that, I need to specify the parameter here. So I already had a flight and I can access that flight ID by typing in-flight untidy. And so now all I have to do is just save that. And let's go ahead and refresh our page and go to flights. And so now you can see that I have a list of floods. Wherever flood. In fact, it's a link which will take me to a new page. Now if I click on any of those links, let's say New York to London, I am taken to that flight. I have information about that flight and the passengers inside of that flight as well. And now if I go back to flights and I'm back to that main page. And so this is one way of having to use these many-to-many relations to use other tables. 28. Adding Model Objects via Webpage: We've seen previously how we could add model objects, like add a new flight or a new passenger to our database through the admin application. However, we will be seeing in this video how we could add, let's say a passenger via the webpage without having to go to that admins site. Because as you know, the user won't be able would have access to go to that Alan page. That page is only used for the programmers to visualize the database. However, as a user, you want to add information using the webpage only. And so in order to do that, the first thing I need to do is add a form and to my fly dot HTML page. So it shows me the passengers that are already in my flight. And below that, I'm going to first add a hey, adding sorry, that says add passengers and below it, I'm going to add a form that's going to let me add the passengers that I want, so forth. And of course that four is going to take whatever wherever I submit that form, that forms going to go that specific URL that is specified inside of that action. And that URL is going to be specified through the name. And so it's cool book that you'll be creating in a moment. So let's go ahead and close that. And now we have that URL. And now we're going to add all the necessary information inside of that form. So let's just go ahead and add that URL, which is book, which is also pi. And so that path is going to be that flight that I would like to access. So Flight ID, which might be one or two. So one slash books. So that flight, which, which is in here, I'm going to access it and then I'm going to build a passenger to that flight. And so whenever I go to that URL, I'm going to go to views dot book, which is a function I'm going to be creating in a moment. And name equals to the book. Let's add a comma. And so now we've added our URL. Let's go back to flight at HTML. And so one thing we need to add to our URL as that flight ID, because we want to book a passenger interspecific flight. And so we need to add that argument because inside of our path is already as a parameter. So if you want to use that URL anywhere, we need to pass, but also that on Flight ID parameter. So instead of flight that HTML, I'm going to enter that ID, which is flight dot id. Because flight as a variable that I can access, so I can access its ID. And so now I need to also add a method to my form. And that form is going to be a post because I'm going to be submitting information. And whenever I have a form in Django, I need to add a CSRF tokens. So this one, this is one of the most important things we need to add just for security to make sure that your form is really valid, as in you are submitting the form and not somewhere else. And now let's go ahead and add a drop-down list, which you can use in HTML. And we could create that field. We'll give it a name of passenger. So the user knows that the adding a passenger ID is going to be the action that's important. So now my slides, I would like to add all my options as an all the passengers that I could select from them and add them to my flight. And in order to do so, we need to have access to all the passengers that are not on that flight. And in order to do so, we need to go to views darpa and create our function. So you recall in URLs apply, we've specified the route and specify that whenever we go to that route, I'm going to be going to use that book. And so we haven't yet created that book function. So let's go ahead and create it. So I'm going to create that loop function, which is going to take as an argument not only the west but light idea. Because if we see that, we can see that that function needs to have a flight ID as the integer variable, integer, integer viral. And so now inside of that function, what the first thing we'll do is to first get that flight. But remember from before that there are multiple ways that I can access. Sorry, I can request a web page. So before identified, I can request our page via the get request method. So when you enter a URL into the webpage, you are actually requesting a webpage via that getWidth method. Or I can request that method via post weaning. I would like to send data to the page. And generally speaking, anytime we want to manipulate the state of something, especially manipulating our resumes, that should be inside of a post request. I'm submitting some forms on data. And in response to that post submission, you should manipulate what's going on inside that database. So going to first check when this book route is called upon. The request method is post. The request method is going to be popes. So we're going to requesting that method, which is going to have to be equal to. Post request has a method, and this method should be bulls. And the first thing we need to do is to get that flight. Flight equals to flight dot objects that get primary key equals supplied ID, which I have access to. And so now I get my flight. And now what I'd like to add now is that so when someone submits this form to a book, to book and you passenger on a flight, you should tell me what the ID of the passenger is. What passages should iBook on that flight? Because these two are pieces of information that you need to do an action to an order to book a flight. The flight and the passenger information. Now we have a flight and we only need the passenger's ID. And so instead of my HTML, we've created that passenger name. And I'm going to be selecting from this list of passengers. And so inside of that function, I'm going to sorry, inside of that form first, I'm going to submit that form and submit to the the name. So the passenger name which is here. So that would be submitted instead of y and form. And so now I can just go ahead and request dot post as whatever I've submitted, which is which happens to be called passenger passage here. And we're going to be fixing that HTML whenever and a moment, sorry to have it give us an ID. Instead of giving us the first and last name of the passage. So for now, let's just give it a name of ID. And that ID is going to be saved by writing the command which is requested method. I'm sorry, the outpost of a variable called passenger, which is inside of our four. And so this might be a string, and we want our ID to be an integer. So let's just go ahead and put it inside of a built-in function called int, which is going to convert it into an integer. And in order to get the passenger, the same way I used fly that objects that get pK of Flight ID. I'm going to redoing the same. Passenger is going to be passenger. Sorry, dot objects that get. However, I don't want to get. So I don't want to get the passengers with that are on that flight. I want passengers that are not on that flight. And we'll see how we could do that in a while. However, you can see now that we have a yellow line below passenger. And that's because we did not import that passenger class. So this is go ahead and import it. And now it's working fine. So. And actually we're just going to be adding for now just one more passenger. And so passenger equals two passenger dot objects that get. And this is the id. So pk equals to end. And so this is going to give us the primary key. And so now what we'd like to do is add that passenger to that flight. And so in order to do so, I'm going to be accessing that passengers, flights. And so in order to do that, I can say passenger, which is the one that I just got that flights, that ad. And so I'm going to add that flight. And so flights is that variable. And that models that we've created, which is going to give me all the flights that the passenger is on. And that light is going to add that flight, the table flights into that table flight. And so now what I'd like to return is a HTTP response, sorry, response, redirect. And then reverse slash Django. Get me that URL. And so reverse book. And so whenever I add a new passenger, I want to return to that same page. And so because books are requires from us to a parameter which is the flight ID. So reverse flight. So reverse flight, I want to return to that flight, which has all the information about the flight. And so since that flight also requires a parameter, I'm going to add a comma at it stay, it takes an argument of flight ID. And I'm going to add a comma because this is a tuple. So in order to know that this is a tuple, I need to add a comma. And now the last thing I need to do is to import from Django dot SCTP. Sctp. I'm going to import that HTTP response to direct. She's right here. And from Django dot URLs, I'm going to be importing reverse. Save that. And so now if I go back to fly dot HTML, you are saying that we need to get all the passengers that are not inside of our plane or flight, sorry. And so in order to do so, inside of views, and sorry, a set of light, which is the main page we our own represents our ongoing you passing another parameter, a set for the passengers, which is the non passengers. And so Patna passengers. It's going to be all the passengers are that are inside of my passengers tables, excluding those that are inside of my flights, which is flight. So flights is a variable inside of that class, which is the passenger class. So this is at a canal and I want to exclude all the passengers that don't have that have, sorry, this flight inside of their flights variable. And so that's passenger now passengers. And so now I've added a parameter to my flights, flight that HTML, which are the passengers that are not on that flight. And now inside of flight at HTML, I can access. So I can create a for loop first. So for every nun passenger inside of my non passengers list, I'm going to give an option, which is generally inside of our option. And the option value is going to be reading the passenger's ID. Because ultimately when I submit the form, what I care is about what the ID is, which is the yo, the user's ID. Of course, the users who are looking at the page and they don't want to see the ideas. They wanted to see people's people's names. And so the value is going to be the idea and the one appearing to the users is going to be the passengers themselves, the name of the passengers, and so on. We're going to so we will remove the non dash because we cannot use it. So now the values going to be passenger, that ID. So that's tidy. And here we're just going to be plugging in passenger, which is going to give us the first and last name. So let's go ahead and save that. And one last thing we need to do, because we changed that to underscore inside of user file. We need to change that too. So let's go ahead and save. And now let's refresh our page. Go to the first flight. And now you can see that we have new section, which is that passengers in which we can choose from all the passengers that we have inside of our passengers list. So here are all the passengers that are not enough light that we can add. So let's say I add this prior and we can notice we forgot to add a submit button. So let's just go ahead and added. So instead of a form, I'll be adding a submit for an input of type submit. So that's the input of type submit. Unless it says save and refresh. And now let's say I address prior and submit. Okay, so now we have a multi-valued carer. And so you can see that it's right here. So let's go ahead and see why is giving us that. So we're requesting from that form an inquiry that has a name passenger variable, which has the name passenger. Subs go to fly that HTML. And instead of R Value, select name is passenger. So that's the problem. So we have to they have to have the same name because when we were taking that passenger that we selected will have to use the name holding that variable. So they have to be the same name. So the passenger is the same of that here. And the now if you do add and, and one last thing, I just forgot to add the dot o. So because it's passengers that objects at all, but excluding flight SQL to fly. So now if we go ahead and refresh our page and trees prior and submit, I can see that now I have CRISPR as my passenger. And you can see that we are redirected to this page using the HTTP response redirect. Let's say. You can see that now trace is not longer on my list because she's already on that flight, let's say at Caleb and now I have caleb entries on my flight. And so you can see how we we are able to manipulate those data with that actually go into that admin app. We were able to add those passengers to our database in that webpage through through that fuse at HTML. Sorry, through that function, which is instead of our view sub pi, that helped us enter data from our webpage to our database. And so that's it for that video. 29. Authentication: Until now, jangle has given us a lot of features, the ability to present models very efficiently with immigration method, to be able to apply those changes to our databases. And now we'll be taking a look at this idea of authentication. That on many websites you want some method of authentication. The ability for users to be able to walk, to be able to log in and out. So Django would remember which user just logged in. And what we're going to do now is introduce an application that lets us interact with this authentication method. Because Django has a lot of authentication features. And we'll be diving into that framework that will do the job. And so we don't need to write a logic. Django is going to do this for us. And so Django, as I said, has a lot of of these features that we were doing for us, everything. And so if we go ahead and hit our airline project, and I'm going to be creating an application that's going to be doing that authentication. So inside of my airline project, I have my flights application. And what I'd like to do now is create an application that's going to, I'm contain users that are inside of my application. So I'm heading to my command prompt and creating a new app. So Python managed up pie, start up. And let's call this app users. So we'll have flights and now we have, will be having sorry, users and other application called users. And it's inside of our project. And so as before, when I create any application, I'm going to go to Project, oop, sorry not Project one airline. And then setting stop by. And then at this new application into my install apps. So users and comma. And of course, I need to add this user's application to my URLs. So the same way I added floods and going to adding users, the path of users. So users. And here we are going to be including users. The URLs of the user, which are associated with my user application. And of course you need a comma. And so now we can save it. And now what I need to do is create those URLs. And so I'm going to go ahead to users in URLs. I'm going to be creating a new file, sorry, called URLs dot py. And so inside of we are as a pi, we need to add, let's just go ahead and copy same code from your PI and just modify it. So we need to import, use and paths. And so inside of our URL. So you are as a pi, we, of course we have different paths. And so we have. And the main index, which is, which is named index. And now we'll be creating a new path which is logged in. And this login path is going to take us to fuse that log in and we recreating that in a second. So login view, and let's give it a name of log N. And of course will be i comma. And now let's add path log out, which is another path comma. And let's go into Datastore views, that log out view. So we'll be also creating that in a moment. And let's give it a name of log out. So let's fix that logo. And that's it. So let's go ahead and save that. As we've said. The first path is the main index route that she's going to be displaying information about the currently signed in user. One for logging someone in, and one will be displaying some something to play, the user and his password. And one for the user to log out from this application. So now let's go ahead and actually write these functions. Console. To write these functions, we need to go to views the pi. And so we need first a function called index. And of course, let's have an argument called request. And inside of that function, we need. And so what do you want this function to do? It's going to display information about the current design in news that I signed into this website. And then I'm represented with this index page. Because if we think about it programmatically, we first need to think about what should happen if someone tries to access this page, but you're not authenticated? How would we even know that? And what do we do in that situation? Well, let's say on if not user dot is that so this is request that user, so it's not request dot user is authenticated. So if this user is not authenticated and a double column. And so what I'm saying here is that the request object that gets passed as a part of the request for every user in Django automatically has a user attribute associated with it. And that user object has and is authenticated attribute, which tell us, tells us if the user is signed in or not. If it's, if he's not saying n, we're going to type in, you're going to be returning a HTTP response. So. She HTTP response through direct. And what I'm going to be doing is getting the reverse of the login function, the path, sorry, that we've added in your as a pie. And so here, Rm, Django dot HTTP as we need to import, of course, HTTP, response redirect. And we need also to import from Django. Url's will import of course, reverse. So now there I've imported this and so this HTTP, not s. And now we important d's. And so if the user is our other indicated will be redirected to the log in button. And so now let's go ahead and create that login view. So the slogan view's going to be taking a request as we wont to be returning for us a rendered template. Upon that request, of course comma, let's call its template Users slash login, that HTML. And let's go ahead and save that. And now let's create our templates. So this is a templates folder. And inside of templates are recreating users a file called users. And now we'll be creating layout HTML, which is our main HTML page. And inside of that layout will be adding a blog, the body block. Let's call this as even the title User. And block, which is called Buy. Now, let's go ahead and create a new HTML5. Lets call it login at HTML, which is going to extend from that layout.html. And the block will be inserting that block and which is called body. And real, we will be adding our content. And so inside of my block, I'll be adding my HTML form. So that four going to be having an action and a method. And the action is going to go to that login view. And in order, no. So this is the layout, the HTML. Sorry. Yeah. So in Laguna HTML, I'm going to be giving it an action which is a URL, which is called auxin, which is the name of the path that we gave inside of URLs dot py. And now it close the parenthesis. And we need specify what method which is an, obviously it's going to be post because we'll be receiving information when we submit that form. So as I said. There is the form, this is an HTML form. And when generally doing that, we need to have post. So because we're receiving information and we need to, we need to and away get the username and password. And so to do that, first things first, we need to add a CSRF in order to be safe place without form. And now we need an input in order to add that username. So it's of course type text and name, username. And we don't need that idea. We just add a placeholder which is called username, in order to let the user know that he has to add usury. And so the name is just a name for us that we'll be using inside of use pi. It's not visible for the user. Visible thing is the placeholder now will be typing in the password, which is of type password. And so the difference between text and password is that the characters would be like bulletpoints, like circles. You won't see what the characters all. In the case of password, Let's give it a name of password and a place holder of password for the user to know that this as a password. And now, of course, we need to give an input which is submit in order to submit our form. And let's give it a value of log n. So whenever we click on that button, which is called log n, will be submitting that form. So now let's go ahead and open and run our server. So Python managed up by run server. And now, of course, we can see now that we have given an error. So we don't have a log out you attribute. So that's because we did not add it. So let's just add that log out new function. So logout mu i, which is going to take in a request. And let's just put it pass for now. Suppose will not do anything. It won't affect on our function, but it won't give us an error because we did not write that function. So this is that log out view that we've called inside of or URLs or pie. Now, if we go ahead and refresh, you can see that the server is running normal. Now, let's go ahead and go to admin page and see if we can. Let's first of all, of course, shrunk login with the initial username and password you have created in the beginning of the lecture. And let's just add some user names before we go to, sorry, add some users before we go to that login page that we created. So let's add a user. Let's say I had John. Let's give it a password. Let's give this username and password. So whatever you want, you can uncheck the password. And of course we need to save. And so you can see that now John was successfully added. We can give him a name, firstName, lastName. And of course we can give him an email address. So walker at example.com. And so now we go ahead and save that. So let's go ahead now and logout and go two log n. So of course, me need to go to first users than then slash login because log n is inside of our application called users. And so now we can see that we have another error, which is template does not exist. And that's one major error that you might do, which is not specifying where that layout.html is. So it must be in Users slash layout.html. So now, after refreshed, can see that we have, since I'm not logged in, I am represented with that log in. And I need to enter my username and password if I were actually logged in. Now, that would give me a lot out instead of log N. However, let's say I write anything inside of username and password, you see that nothing happened because we haven't yet implemented that login method. So let's go ahead and add some code to our function in order to make it functional. So the first thing we need to do is this login view function could be called in two ways. One via the get request method, meaning just show me that login you form. And one is via post, send it to that login form. So via the form will be calling this function. So if request.me is so we need the Post not that get. So if request method is post, I'm going to be doing, doing a couple of code. And so that's something is trying to get that username and password. So I can get the username by getting from that request. I'm going show that form, dot post. So that form that I've submitted, I'm going to be getting from it a variable that is called username. And that happens to be that input, which is usury, which has an English name. And we'll be doing the same thing for password. So password equals to request that post password. So now we have the username and password that the user has submitted inside of the form. And now what I'd like to do is try to authenticate the user. And how do I go about doing that? Well, it turns out that there are a couple of functions that Django has given to me that I can import. So from from Django dot sorry, contract. So contract that authenticates. I'm going to sew off is for authentication. And we're going to be importing login function. And so we have a login and we have a log out function, and we have the authenticate function. So now these are three functions that will be ultimately using. One is to authenticate that user name and password, right? And so one is called Lock and to login the user and one is luck out slug or the user. And so after our gut that username and password, I'll be trying to authenticate that user to check if the username and password are correct. And to do so, I'm going to create a variable called user, which is going to re-authenticate. That will be using that authenticate function in order to enter request first and then the username, which is a parameter which is going to be equal to username that we've got from that form. And that password too, which is also a parameter, is going to be equal to that bus route that we've got from that form as well. And so as I said, it's going to be taking that username and password and upon that request. And it's going to see if that user is actually a authenticated user. So and if it is authenticated, is willing to give me that who this user is actually is. And so if this user is not null, so if it's actually a user that has a name and a password, I'm going to go ahead and log n upon of course, that request. And I'll be going to belong in that user. And now let's go ahead and return to the main page, the index page. So return HTTP response to direct, reverse. And I'm going to go to the next, which is the name of my path. So and we've already imported them, so we need to import them again. So else, if that user is actually none, I'd like to return to that that login view. So I'm going just to copy that. However, this time i'd like to carry on a parameter. So this parameter is a message which is going to say that your credit, your credential, or wrong. So in correct credentials. And of course we need to add them between strings. So now we will be saving it. And so now inside of our login dot HTML, I can say something like if there's actually a message. So if message, I'm going to be printing that message. So if message I'll be just showing that message. Now, let's go ahead and see if that actually works. So now you can see that because I've refreshed and you can see that I've added credentials and as incorrect. So it actually worked. However you've saw You've seen that incorrect because I refreshed. So now, if I redo that, I enter anything, I can see that given with incorrect credentials, as in this user, is not actually a user inside of my application. So now let's go ahead and continue my program. So we actually said if the user is not authenticated will be going to that log n. So in order to authenticate, however, if the user is actually authenticated, I'll be returning to render template, which is sorry, render upon the request. And it's going to take us to fix that indentation. And let's say users slash user_data HTML, so that user is actually authenticated, will go and see some information about that user. So let's create users. You saw user_data HTML. It's known to be of course, extending layout that users slightly a stat HTML. And inside of that block called body will be adding some, some information about that user. So I can say something like each one. And inside of my heading one versus c will come user. So inside of curly, double curly brackets, I'm going to be taken from that request, that user. And we can, of course, access, let's say his first name by typing in a parameter which is firstname. Now, after we do that, after we will come that user, we can add some information inside a small heading, let's say edge two and username. At, just enter his username by adding requests, that user dot the parameter which is called username. And let's say now we also want to display his email. So immunologist request dot user, dot e-mail. And so now that we've done, let's go and save and go to use a pi. This is our index which is, let's go ahead now and refresh. And now let's try to Andrew about credential and of course with their password. And you can see now that we are returned with welcome John. John is the FirstName, and that's his username and email, which we've added into our admin interface. And that's because John is logged in. And when we request that information of the user, we're requesting from that logged in user, which is John. So we're accessing the properties of that user. Now one last thing we need to do is actually log out whenever we log n. And in order to do that, as we've said, we have that log out function that Django has created for us. And so we don't need a supplement that logout method ourselves. We just need to add it to our function. So our logout function. So we'll be removing bus and just replace it with log out that user by requesting that user. So be returning, be returning to login. So I'll just copy that. And I'll be passing as a message, another message which is logged out. So you've recently logged out. And now inside of index HTML, sorry, inside of our user html, the adding that link that will take us to that log out pitch. So I've added a URL to that function, just log out. So logged out. And this is the name of the path which will take us to that function, that login function. And let's just give it a name. She's logged out. So remember we click on log out. You can see that it's here. So when we press the logout, we can see that we are locked out and we'll return to that log in page. So now if we grind and resign, and you can see that now we are resigned. And so we can logout login from that webpage. So Django has given us a lot out of the box. It gave us the ability to represent these models in admin interface, to be able to manipulate them. And migration system that allows us to very quickly make changes to our models and apply them to our database. And also a both end user authentication system. A system that allows us to very quickly enable users to be able to login logout from our application as well. So all of this are features that are just here to help us make it so that we can very quickly take advantage of things like SQL and models and migrations to bold, dynamic, interesting web applications with data to back them up. So this is actually the end of the course. I hope you actually enjoy scores with all these new methods in order to create a web application using Django. I hope you did not face any difficulty with any of the Coase F, So please add it into the discussion. I'll be more than glad to help you and guide you through the process. So this is Django and Python for Web Development. Thank you so much for reaching the end of the course. 30. Project: Now we reach the last part of this course, which is the project. And the project is pretty simple cause we've done a similar example on this throughout the course. And so what I need from you to do is move up a task application, which is really similar to the reminders application that I built. And of course, the first thing you need to do is create a project. And you can call this project widgets and then create an application obviously, and you may call it, let's say tasks cosmogony to creating a tasks application. And of course, we need to link that up to our project by adding it to our install apps, creating, sorry, adding it to our URLs dot py and then creating a URL, so PY file inside of that application. And we have seen how to do that throughout the course. And it really degree symbol. We have to first display a page that displays the tasks and a page that displays and do tasks to add tasks. So in order to add a new task and of course display task, we need to have an HTML page that looks over the tasks or over all the tasks. We need an index method inside of our reviews of pie to access the HTML page. And of course, pass a variable. The variable tasks as a parameter. And we need to have a session so that each user has his own list of tasks. And of course, we need to have like a link that takes us to add a new task. And so when we go to that and new task, we need to have an original page that contains a form to submit then you task. And that we, for that we need f form class inside of that form. And we've seen how to do that. We've seen actually had to do all of these things. So this should be pretty easy for you. And so we need to create a Form class that contains that variable task, which must be a character field. And of course we need a link to get mapped to the index page. And we need to add inside of our used up by an add method in order to get the data when, after we submit the form via post. So when the method is post. And of course after we've done all of these things, we need to add a new task to our list of tasks. So that's the project. It's actually pretty simple. If you have any difficulty, you face any difficulties with the project. You can write that in the discussion of the course or you can contact me. However, I don't feel that you'll have difficulty doing it. And so by now, I think you've reached the end of the course. I hope you enjoyed it. You understood everything. If not, please write that in a discussion, we can discuss whatever points you didn't quite understand. Thank you so much. That was web development with Django and Python. My name is Sarah. Thank you.