Introduction to URLs and Views
Hey gang, welcome to your third Django tutorial. And in this video, we're gonna take a look at URLs and views. Alright, so before we jump into the code and just start coding, I want to talk about how URLs and views are gonna work in Django.
So imagine we're in a browser and we go to a website and make a request to the /about URL, okay? So we expect to see maybe an about page returned to us in the browser. So we send that request, and the job of the urls.py file in Django, and we saw that briefly in the last tutorial, is to look at this request URL and then decide which function to fire in our Django code within the views.py file, okay?
So say for example, we go to /about, the urls.py file is going to look at this URL and say, okay, I want to fire a function inside this file, views.py, called about. And that function can be called whatever you want, but it makes sense that this is called maybe about, okay? And inside that function, we can control what happens when a user visits this URL.
So ideally, what we're going to do is send back some kind of response to the browser. So inside that about function inside the views file, what we do is say, okay, send a HTML template or some kind of response to the browser, send that along for them to see. And that's how this triangle kind of works. That's the the crux of URLs and views inside Django. So now we know basically how it works, let's go into the code and start creating some URLs and views.
Project File Setup
Alright then. So the first thing you might have noticed is that we have this dude just chilling out inside our Django Notic folder over here. Now, this is automatically generated by Django. You don't need to worry about it. It essentially just speeds up our application when we're running it, and you don't need to delete it. Just leave it as is for now.
Okay, so I said what we're going to do is start configuring our urls.py file and also our views.py file. Now, we can see the URLs file right here, but there's no views file in sight. So we'll come to that in a minute. And now let's pop in here. And all this yellow stuff at the top, that is just some comments by Django to tell you how to use this file. But since I'm going to show you, let's just delete it all to make it look a little neater. Cool.
Understanding urls.py and Regular Expressions
And the first thing you'll notice is that Django has imported two things up here: URL and admin. This URL we need to create URLs down here. We'll use that in a second. And admin is used in this callback function right here. So Django when you first start creating a project already sets up this URL for you, okay, the admin section. Django comes shipped with an admin section which we'll look at later and has created a URL for that which is just /admin.
Alright, so what we want to do now is create two more URLs, one for the about page for example, and also one for the home page. So how do we do that? Well, all we need to do is come underneath it and say URL, and we're going to do pretty much exactly the same as this thing here. We have two parameters. First of all, we have a raw string, and that's what the 'r' is for. And this is going to describe what the URL is, right? And this says right here 'go to /admin'. This caret is a part of regex or regular expressions and it just means that this has to come at the very start of the string. It can't be something like /blardy-blardy-blar/admin, right? It has to be at the start of the string, admin. So it has to be /admin. That's what this thing means.
Right. Occasionally you'll see a dollar sign somewhere here at the end, and that means that this has to be the end of the string, okay? It can't be admin/blardy-blardy-blar, it has to be admin. So this right here is known as a regular expression. You don't need to know too much about regular expressions to survive in Django, but we are going to look at them a little deeper as we go along when we start to construct more complex URLs. Right, and I'll talk about it a bit more as we go along. For now, all you need to know is that this caret means the start of a string and the dollar sign means the end of a string, okay?
Defining URL Patterns
So what we want to do is create an about page. So the first parameter is a raw string. So we'll say R, then inside the string we want it to be caret because we want it to begin with about/ and then I'll put a dollar sign at the end, which means this has to be the last of the, or rather the end of the string as well, right? So it has to be just /about rather than about/something-else, right?
So the second parameter right here is going to be a function. Now we're going to come back to this in a minute because we don't have our views.py file set up. For now what we're gonna do is just another URL which is going to represent our homepage. So inside here again a raw string, this time we want it to be the home page. Now when we go to a home page, it's normally just whatever.com and that's it, just forward slash, right? So we don't have anything after here. So how do we do that in Django? How do we create this regular expression or string to denote that we want to look out for just forward slash, just the home page? Alright, or it doesn't even have to be just forward slash, it's just .com sometimes, right? Well, the way we do that is just by saying caret and then dollar sign with nothing in between it, meaning this is the start, this is the end, there's nothing in between, right? So this is gonna look out for just .com, right, whatever.com or whatever.com/, so that's how we create a URL for the home page.
And again, let's do a comma and then right here we'll do a callback function much like we have here which is going to point to a function inside our views file, right, for the home page. So let's say your URLs are configured. The next thing we want to do is go into our views file.
Creating the views.py File
Now we don't have that at the minute, so let's create one. I'll right-click over here and I'm going to call this views.py. So that is in this Django Notic folder next to the URLs now, right? So inside this views file is where we create the functions which will fire when a user visits a certain URL.
So the first thing I want to do is import something, right? And that's going to be from django.http I'm going to import the HttpResponse module. This is going to allow us to send a response to the user, right? So when they request a page, we're going to send them something back. All right?
So now we've got that, let's define a couple of functions. We need two functions. We need a function which is gonna fire when a user visits this URL and a function which is gonna fire up when the user visits this URL. Right? So let's do the about one first of all. I'm going to say def, which is defining a Python function, and this is going to be called about. Again, you can call this whatever you want. I'm calling this about because it makes sense that visiting the about URL, we want to fire a function therefore called about, right?
And inside this function, we take a parameter called request and that is the request object, if you like, the the thing that's given to us inside this function when a user visits that about URL, okay? It has information about the request that was made. Now, all we want to do when someone visits this URL, /about, is send back a simple HTTP response, right? For now, we're not going to send back a page or HTML template, we're just going to send back a simple text HTTP response, and we'll look at templates later. So the way we do that is by saying first of all, return, we're returning something out of this function. And it's going to be an HttpResponse which we imported up here, right? And then inside this HttpResponse we'll send through a string and this is going to be 'About Page'. Okay.
Connecting URLs to Views
So we have that function now for the about page. So we can hook it up inside our URL file. How do we do that exactly? Well, the first thing we need to do is import this views file, right, into this file because we're going to reference it. So I'll say from . I'm gonna say from . that means from the current directory that you're in. So views.py is in the same directory as urls.py. So we're grabbing from the same directory, and we're importing from that directory views, right, which is this file right here. So we've imported that file now and therefore we can reference the functions inside this file. So I can reference this about function right here.
So I can say in here, well the callback function I want to fire when someone goes to /about is going to be views, which is this file we've just imported, .about, which is the name of the function we created right here. So this function is gonna fire when someone visits this route or this URL, okay? Makes sense.
Let's do the same thing for the home page. I'm going to create above this a new function called home. And this function, again, is going to take in... in fact, I'll call it homepage. This function is going to take the request object again. And inside the function we also want to return an HTTP response. So I'll say return HttpResponse and then inside we're gonna send a string again and this can just be 'homepage'. Alright, so now again, let's save this and hook this up to our home page URL.
So this time we want to say views.homepage to fire that function when someone visits just forward slash, right? So let's pop a comma on the end of that, save this, save the views file, and let's run this.
Testing and Recap
So we'll go to our command line tool and we'll say python manage.py runserver. Remember, this is how we spin up a local server for our project and how we can view it in the browser. We want to go to this address right here which is localhost 127.0.0.1. You can type localhost instead of that port 8000. So I've already gone to there inside this browser. I'm going to click enter just to see this and we get the home page. Right, so we've gone to just forward slash, and we get the home page. Awesome.
Now, if we go to /about, then we get 'About Page' sent back to us. Alright, so this is really cool. What we're doing right here is we're looking at the URLs when they're sent, when they're requested in Django in this file right here, right? And we see that, okay, someone sent for the about URL, therefore I want to fire this about function in the views file. So these functions and we send back an HTTP response saying about. Makes sense. Same for the home page. When someone visits just / or just .com, then we send back the views.homepage function which is going to return this HTTP response right here sending the text 'homepage'.
So there we go, my friends. You've successfully created now two URLs and a views file which is controlling the output of data to the browser. Alright, so that's a good start. So now we know how to do this, in the next video, what we're going to do is take it one step further and have a look at HTML templates and how we send those to the browsers instead of just text.