7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 Create Router URLs

The main entry points into your Django application are described with URLs. These URLs take the form of regular expressions that describe the basic structure of HTTP requests and where those requests get serviced within your application. Trust me, it's actually much easier than it sounds! In this lesson, I'll show you how to create these URLs.

2.3 Create Router URLs

In the previous lesson, we learned how to take those models and then ultimately migrate them into the schema of our database. And then be able to create a super user that can log into our administration pages. And then be able to take a look at these models. And then add different instances. Or remove them or modify them or what have you. And that's all really good, but the problem with that is it's a little bit limiting. Because at the end of the day, the administrative access is gonna be restricted to only a few different users. What we wanna be capable of is having any random end user kind of come into our system and be able to take a look at these articles that we find, within our site. But in order to do that, we're gonna have to create that kind of landing page or those separate pages that they can come and look at that aren't restricted to administrative users. And the data that they're gonna see there, well right now there's really nothing to look at. So in the previous lesson we created a feed. So what I wanna do in this one is, let's just create an article so we can start going down the path of. What it's gonna look like to actually have an article that is associated with feeds. So the first thing that I want to do is I want to associate this with the feed that we previously created. Then, I'm gonna have a title, we'll just call this my article. And the URL is going to be myarticleiscool.com. And the description is going to be, this is a fancy description, like that. And then we'll just say that it was today and now. So that's basically it. So just basically throw in a little bit of data It doesn't have to be too fancy, just something that we can start with. So let's go ahead and save that. So now we have an article that's associated with a feed. So this is good. This is a good start, but we need to be able to get up to a place where the user can actually navigate to a page and see it. And when that happens, they're gonna actually type something in or have some sort of link that's going to appear in the address bar of your browser. And as you see here, this begins with admin and then it says news and article and things like that and that's all coming from the different pieces of the application that we're building. But the part to pay attention to now that you're going to see in a moment is this Admin here. So what we need to do is just like this is working now, when it sees when our Django Application sees that we want to go to something admin than its handling this request into our application somehow. And that's what we need to kind of get into in this lesson so you can see how that it's going to work. So the first thing that we need to be able to deal within our application is going to be handling those URLs. So let's go ahead and stop our server right now. And we'll head back over to our text editor and what we want to look at for the time being anyway is we want to take a look at within our project there is a urls.py file and this is where that whole configuration of those requests coming in to our application for a specific URL is being handled. So if you recall within the browser, we see that we have this admin going on here. Well that is mapping to this admin right here, within this URL's pattern. And really, within urls.py. What's happening here, is we are taking different patterns of these URLs in a rather generic way and saying every time a request comes in that looks like this. I want to handle it in some way. And that's where this is actually coming in to play. Now, the way that you define these patterns is via regular expressions. Now, regular expressions can get fairly complicated but we're gonna keep it rather simple in this course because it doesn't really need to get complicated. We're gonna try to keep it very simple, but all this means here, this little carrot sign means we're going to start at the beginning and say if this URL starts with admin and has a slash in it like this, then this is going to be handled in a very specific way that is defined within the admin site URL's file. So that's kind of the way that this is working. And that's stored off in another place somewhere else that we don't really have in front of us right now. But I'm gonna show you the way that this is going to look, or how it's going to work for us. So now what I wanna do, is I wanna create another URL in a very similar way that's going to take the requests that look a certain way, and forward you off to another place. That's gonna handle those requests. So I'm gonna create another URL, and I can create as many of these as I'd like. The r really just says that we are dealing with a regular expression here. And I wanna say when I get a URL that starts with news/, then I want to include News.urls. And that's basically going to say at this point, that if a URL looks like then, then I want to take that request and I want to kind of forward it off to this file here, news.urls. Which is a very interesting way to name it since this is a convention used within Django and Python to say this is going to be within the news directory or in our case, the news app, within the urls.py file. Well, that's all good and I can save that. But if I come in here and take a look, that doesn't actually exist yet. So that's something that we need to create next. So we're going to come into our news directory. And we'll go ahead and create a new file in here, and I'll save it just so that we don't forget about it. And we wanna make sure that it's in our news directory. And then we're just gonna call this urls.py, just like that. So we'll go ahead and save that. So now there's nothing in here for the moment, but we're gonna change that very quickly. So the first thing that we need to do within these URL files is we need to handle these URL's, and so they way that we do that is we're going to reach into Django, so we're going to say from django.config or .conf.urls. We want to import URL. You're gonna see that used here very, very shortly. Now the next thing that we're going to do is we're going to need to define our URL patterns. So, we're gonna say urlpatterns is going to be equal to, and this is going to be a list, and this list is going to consist of URLs and that's why we're importing this URL function into this particular file. So now within here, let's just make this very generic and we're gonna say we're gonna create a regular expression. And now at this point, we should assume that once we've gotten into this file that we know that it has that news at the beginning, so we're kind of past that point. So now we're gonna look for anything that comes past that news section. And so really to get things started very simply and very quickly, I'm just going to say carrot, which means from the beginning. And the dollar sign means to the end. That's it, so if you think of it this way the ^$ basically just means anything. So when you come in here this is what you're gonna deal with. This is the URL that we're expecting. Now what do I wanna do with this? Well the next thing that we're going to do, is we're going to define what's known as view, what's known as the piece of functionality within our application and it's going to present some data. So I'm going to define this view the same way that I just defined my URL within the previous file like I did down here. So what I wanna do is I'm gonna create a view. Now assume this doesn't exist yet because it doesn't. So we're going to say views and we're gonna call this articles_list. And this is just a convention. You can call it basically anything you want. And then we're gonna give it a name of a very specific name that we can refer to this as and we're just gonna call this news_list. Like that. So once again we are defining the URL that we are expecting at this point. Which is basically anything at the beginning. Anything at the end. Then we are going to say that we want to forward this off to a view that is called articles list. Which has a name of news list. And actually just to be consistent let's rename this as articles list like that. So let's go ahead and save that. So right now we've created a bunch of these URLs and let's just see where this has gotten us so far. Let's see if we can actually start to put some of these pieces together. So let's clear this out and let's go ahead and run our server again. So we're gonna come back to our web browser and now what I'm expecting is if I come in here and say news. Well something good happened, and I guess something bad happened, but what exactly is going on here? Well as we started to go down the path of creating these URLs, we kinda skipped over something and we kinda were following a convention in a file that was already there but it just so happens that it was incorrect. So if we come back in here to our news project into urls.py we're gonna see that we were trying to take any sorta request that comes in that matches this particular String, which is news like we are asking for and we want to include something, but since we are referring to a specific file in the directory structure, we need to enclose this in quotation marks and not use this particular convention, because this convention is used for a specific purpose. That is part of a module called Admin or that is in a module called Django.contrib. So, let's go ahead and save that and now we're gonna comeback over here and let's refresh this page and we're gonna see here that we've come to a point where we now have views is not defined. So, what does that mean, well I kind of alluded to this before that we created this URL's file and we were talking about this views.article list, well this views doesn't really exist yet. So how do we get past that one? Well, that's a very simple modification is we're going to come into our import statements here. And we're gonna say, from our local Module. We want to import views, like this. So let's go ahead and save this. We'll go back to our browser. We'll go ahead and refresh this. And now we're getting a little but further. So we're gonna say that module object has no attribute articles list. So we're starting to say, take these individual steps. It's saying it doesn't see this articles list, it has no idea exactly what this is and that's what we're gonna take a look at next.

Back to the top