Introduction to Django's Default URLs
So right now whenever you just make a standard website without creating anything yourself, you have a couple things. The first thing is this homepage, and this is just the server homepage that says hey, your Django project is working correctly. You didn't mess up your computer, congratulations.
Now there's one other area that it comes with, and that's the admin section. So if you go to this admin section, you're going to see some errors, and that's just yelling at us because we didn't set up the database yet. But basically, we have this section. So for some reason, Django already knows that whenever we type in admin right here to send us to this page. How the heck does it know that?
The Core of Django Routing: urls.py
Well here's the thing. Whenever any user requests something from your website, the very first thing that Django is going to do is it's going to go in this directory, the very first one we created, and it's going to look at this file right here.
And let me close all the other ones. All right. So in here, what you have is something called URL mappings. So basically what it does is this part of the URL, it doesn't really matter. This is just the address of your server, just the IP address or domain name. Whenever I talk about URLs, this is the actual part that matters. So basically, Django is going to ignore this part and look at this part. This is the thing that the user is requesting.
So whenever it gets to this file, it's going to go through, and usually you're going to have a bunch of different ones in here, like I don't know, maybe like a couple dozen. And it's going to go through and it's going to look for these patterns. Now, these are written in regular expressions. And if you guys don't know what a regular expression is, I'll talk you guys through them. You guys probably should like go online and find a quick tutorial and brush up on them a little bit, but I'll talk you guys through them when we write them ourselves. Basically, you're going to look through whatever pattern matches the user request.
Mapping URLs to Responses
Basically, you're going to look through whatever pattern matches the user request. So again, what Django did in this example is it looked at the user's request, which was admin, and it went through and it found a pattern that matched. Now whenever it found one that matched, it said, "Okay, I found one." Now this second part of the function is, "How you want me to respond to that user request?"
All right, simple enough. Not as hard as it looked.
Scaling Problem: A Cluttered urls.py
So what if we wanted to say, "Okay, now we want to say whenever the user types in music to do something else?" We can probably figure this out. I mean, just type in url(r and then we need the carrot. By the way, whenever I type in r, this means a regular expression. Now, whatever I type in here is the actual expression or test. So a carrot just means the beginning. So all of the tests that you write to to see what the user is typing in, what kind of thing they're requesting, it's always going to start with that carrot. It's the thing above the six on your keyboard. So what do we want to handle? We want to say whenever the user types in music in their URL, then what do we want to do?
Best Practice: App-Specific URL Files
Wait a second guys. Before I continue, I'm kind of thinking of something. I mean, all right, what is this website going to be? It's going to be this huge, humongous thing that's going to take over the world. It's going to be bigger than Amazon and Google. I mean, you're going to have people uploading music and videos and software and profiles. And you know what, if we have all of these URLs on our web page, let's say that we had like, you know, 600 different ones, then this file is going to get really cluttered, now that I think about it. I mean, all of our URLs for the music section, for the video section, for the profile, they're going to all be cluttered in here.
So before I even type anything in here, we need to think ahead and think about the design. Now, this isn't actually just me being optimistic and thinking this program is going to be the biggest thing ever. This is actually how you should properly set it up. So whatever URLs you want to be involved with the music section, like maybe add a new album, edit your albums, uh play a song, whatever. You don't want to just add them in here because this is pretty much the settings for the main overall website. All of the URLs, you want to stick in here. And that's the cool thing about these apps. All of the information is contained within that app's directory.
Creating an App's urls.py and View
So that being said, how do we do that? Well, you see how this website has a urls file? Well, let's go ahead and right-click and make a new one, urls.py, in this music app. So what we're going to do is we're basically going to create a list of URLs just for this music section, and then once we have that list, we're just going to import it right here in one line.
So you can actually copy this first line of code and paste it right in there. Now, another thing I want to do is this. You know how I said that most of the time, whenever the user requests something, you just want to give back like an HTML response, a simple view? So what we need to do in this music/urls is we actually need to import them, those views. So from the current directory, and that just means dot, what we need to do is import views. So again, this dot just means look at the same directory that I'm in, I'm in this one, and look for a Python file or module called views. So we're basically taking this and importing it into our URLs. There we go.
Now after this, I'll do that later, but basically in this file, what I'm going to do is the same thing as this. I'm going to make a bunch of rules to check what URL the user was requesting. So again, here let me just code this from blank. So again, everything is just going to be a regular expression and they start with a carrot. And whenever you want to end it, you put a dollar sign. So later on, we're going to be making a bunch of different things like, uh, I don't know, like songs and I don't know, like IDs and all that stuff, maybe delete music, whatever. But for right now, we're just going to put nothing. And whenever we do this, this is going to be the default homepage. This means the user went to the section. Basically, they went to /music/, but you see this, they didn't request anything. So this is actually how you set up the index for each individual app or the homepage for each individual app section.
So after we make that test, if the user wants that, then what do we want to do? Well, we're actually going to make a view in just a second, and we'll just name it index. And that's usually what you do, just because index is the homepage of a section. And this last thing, I'm actually going to explain in I don't know, maybe two tutorials. We need to give it a name, and this is actually optional, you don't need to, but I'm going to show you guys a really cool trick later on of why you want to. And if you guys are confused right now, I'll explain everything at the end, and it's going to make a whole lot of sense.
Including App URLs in the Main Project
So right now we have all of these patterns, but again, whenever the user goes to this, I said that Django is just going to look right here in this file by default. Now right now, this file, it doesn't know about these extra URLs at all. So how do we let this main URLs file know about our music section? Well, what we need to do is in this website urls.py, is we need to import one more package, and that is include. And this is just a package that lets us include other files, which is exactly what we're trying to do. And then we just copy this, because I'm lazy, and we're going to add another rule. In this rule, instead of admin, is just music.
Now instead of this, we're just going to say include('music.urls') right there. All right, so what we're saying is whenever the user requests any web page, go to this, and this is going to tell you how to handle it. It already had the default admin handler right there. And now we're saying, "All right, whenever the user requests anything that starts with music, then how do you handle that?" Well what you need to do is you need to hop over to this file right here. And right now, we only have one page that we're going to set up to handle, and that is the homepage of the music section. So it's giving us a little warning. It says, "Hey, you told me that whenever the user requests this, that I'm supposed to deliver this view. All right, um yeah, that view doesn't exist yet. So can you go ahead and create it?" And that's what we'll do right now.
Creating the View Function
So actually, just go ahead and delete all this. So again, like I said, all the view does is it takes the request and it sends back an HTTP response. So from django.http, and there's a bunch of different responses you can send, but this is the most simple. What we want to do is we want to import HttpResponse. And an example of HttpResponse would just be some basic HTML code or a basic website, whatever.
So whatever you name this right here is the name of your function. So we'll just say def index. And you always pass it in request. And we need to return a response, so just a basic HttpResponse. And inside here, you can just write any text or HTML code you want. So let me just write like a, I don't know, maybe like a heading one. All right, <h1>This is the music app homepage</h1>. All right, looking good mate, looking good. So now let's go ahead and refresh our server. And there you go. Look at that. So again, make sure that admin still works... getting those bugs still, we'll fix those in the next tutorial... and now music goes here.
Reviewing the Full Request-Response Cycle
Now, one last time, I'm going to explain exactly how this works. I'm going to pound it in your brain because if you understand this, then this is probably the most confusing tutorial, then everything else will be a lot easier. So let me just minimize all these.
All right, so the user went to your website and they requested /music/. All right, so no matter what they request, we're always going to hop down and look at this file first. So this is the main urls.py file, and it's going to look for those patterns. Did they request admin? Nope. Did they request music? Yes. All right. So what do you want me to do?
Well, it says include or hop over to this file. So then it hopped over to this file, and it says, "Okay, okay. Now in the music section, what did they request?" Well, nothing really, they just requested this section. So it says, "All right, well I guess it'll just have to be handled with this then." So whenever they didn't request anything, I am supposed to go to views over here and look for a function called index right here. Oh, there it is right there. So what I'm supposed to do is I'm supposed to send back some HttpResponse, and I forgot to put in my extra H1. And all this says is send back the words, "This is a music app homepage" in heading one. So that is how we got that.
Hopefully it is now pounded in your brain. You go there, then there, then there. Boom, roasted. So I know that was a lot, but well, that was the hard part. So thank you guys for watching and I'll see you guys next time.