Developing Websites from Scratch using Python and Django -Part 4 (HD)

Welcome back to Building Websites with Python and Django Hopefully, if you decided to partake in a little bit of food that you enjoyed whatever it is that you ate

I know we did, and in case you're just tuning in, where have you been all day? No, but seriously, in case you're tuning in, again, this is building websites with Python and Django That's Susan Ibach I'm Christopher Harrison, and we spent our morning going through the basics of Django We did kind of a high-level overview, and then Susan did a couple of fantastic demos and walked everybody through how the ORM layer works inside Django Now, it's my turn to babble on for a little while, and we're now going to get kind of more into the webby side of things, and we're going to start that off by taking a look at URLs and routing

>> And if you did miss anything we covered in the earlier module, I saw a couple of questions in the Q&A, something really good for you to know, these videos will all be recorded and available about two weeks after the live date, so yes, you will be able to access anything you missed later and go back and look at it later if due to time zones or whatever, you weren't able to catch absolutely everything >> Thank you for pausing for me so I could get one last drink of water >> Absolutely >> Okay, so let's go ahead and talk a little bit about the concept of routing and what that's all about, which as you're going to see is going to lead us into regular expressions, and then we'll finish it all out talking about configuring routing in Django But first up, let's talk a little bit about routing

So in the days of yore, when it was still things like Linq and Netscape Navigator and all of those good old days, if you wanted to navigate to a particular page, save generally speaking for just whatever the root domain, like wwwmicrosoftcom, what you wound up doing was you wound up calling a specific file So, for example, you might go to http://contosocom/contactus

html And so that would go ahead and give you that static HTML file Or maybe you were going to call something that was going to be dynamic, so maybe it was doing CGI or it was going to be doing Perl, so it might be, for example, wwwcontosocom/createaccount

pl, but the one thing that's common between those two is it's still going to be saying, I want that specific file Well, what happens if maybe you change technology or let's go one step further with this, is let's say we want to be able to pass additional information to that file, so maybe we've got wwwcontosocom/detailsasp, going kind of real old school there, ?albumid=42

The one thing that all of those URLs have in common is that, frankly, they're all bad, because first of all, a lot of those URLs just don't look really good Nobody cares about the extension Nobody cares if you're using HTML or if you're sending out an XML in a style sheet Nobody cares >> That should transparent to the user

The user shouldn't need to know whether you're using HTML or ASP They just want to go to a website >> Exactly, yes, so nobody cares On top of that, when you take a look at something like albumid=42, what's 42? Besides the answer to the ultimate question of life, the universe and everything, what's 42? I don't know, you don't know, and search engines don't know So what we want is we want something that's going to be a little bit clearer to the user

On top of that, if you're pointing to a specific file, what happened if that file location changes, or maybe you change the technology that you're using on the server Well, you're pointing at a specific file, so now you're going to have to figure out how to reroute everything, and I've got better things to do with my time I know you've got better things to do with your time It just doesn't make a whole lot of sense So we want the URLs to look good

Now, you still might be thinking, well, wait a minute, why do I care what the URL looks like? Why do I care about a vanity URL? >> Christopher, why do I care what the URL looks like? >> I'm glad you asked Well, the reason you care is because a vanity URL is going to make it easier for a user to figure out what it is that they're about to see So when I look at this URL, I have no idea what I'm about to get I know I'm going to get details, and I'm probably going to get an album, but that's really the extent of it But if I saw a URL that looked like this, www

contosocom/album/wish, I've got a very good idea that I'm going to be looking at albums and I'm probably going to see something called wish That's what I'm going to wind up seeing So it's much easier for me to see as the user, but here's another big things >> Oh, and you want to talk about one of the things that anybody, when you're working for companies and startups and you're trying to build websites, something you're always talking about is how do I do search engine optimization, and how do I do things to maximize the chance of my website coming up in search results? That comes up, it's a really important thing

>> Whenever I'm teaching a web class at an in-person event, the two questions that I always get asked, number one is some form of a performance question, so it could be is this going to slow things down? How can I make it go faster, etc? And the second question is about search engine optimization And tell you what, I'm going to answer both of those questions in one fell swoop Are you ready? Just go with best practices of just general coding That's really it

So first of all, when it comes to performance of code, generally speaking, the code that's the easiest to read is the code that's going to execute the fastest And on top of that, even if it might not be, like somebody asked in the Q&A window earlier, which was a great question, was hey, if I'm using this ORM, is that going to be slower than doing a direct SQL query? And the short answer to that question is yes, but it's going to be so negligible, that honestly, it really isn't going to matter, and that little bit of a performance hit that you're going to take can be overcome by hardware So I don't know about you, Susan, or anybody else that's watching, but of all the projects that I've ever worked on, of the two which was more important, time to market or performance, every single project I've ever worked on, save for one, it was time to market It was all about I want this right now And that's not to say that performance isn't important

It is We all know that it is But if we have to choose one or the other, time to market or performance, time to market is always going to be more important And you know what? Again, if you're just writing good code, you can get that performance that you want by just adding more RAM, adding more CPU, spinning up another server, sliding the little bar over on Azure to just spin up another instance, and away you go from there In kind of parallel to that, about search engine optimization, how do we make our site better optimized for search engines? Again, it's all about following those best practices, so for example, if you're going to put an image out there, best practice is to add on an alt attribute

Why? Well, because it makes it more accessible for the general public Screen readers now know what it is, but on top of that, a search engine is now going to know what it is So if the user can figure out what it is, a search engine is going to be able to figure out what it is So when I see albumid=42, I have no idea what that is, and a search engine isn't going to know But if we see wish on there, now, we know exactly what that is

We know that that's going to be the name of the album, and so our search engine is going to be able to pick that up So how does this relate back to our discussion of the uses of routing? Well, again, we don't want our users to request a specific Python file, because it's not going to be a specific Python file that they're going to be calling Eventually, it's going to be a method, as we're going to see in a little while And what we really want is those users to request a resource or an action, and then we'll figure out what code it is to execute on the back end So what we need to do is we need to explain to Django, hey, if you see a URL that has that structure, what we want to do is call this code

That's what we're going to be doing here But we've got a little bit of a problem, that if we break down this URL AdventureWorks

I was using Contoso earlier, but whatever it is, this wwwadventureworkscom, that's going to be static

That's going to be a string literal That's going to stay right there Albums we can basically treat like a keyword, that that's going to be static, that if I want to bring up something about an album, it's going to be wwwwhatevercom/albums

That's not going to change But you know what is going to change, is Wish That's what's going to change, because the name of the album is going to change, so it could be Wish It could be Disintegration It could be staring at the sea

It's going to change at the very end So what I need to be able to explain to Django is, hey, look, all of this let me kind of clear my screen

This part right here, that's going to be static But this part right here, this is going to be dynamic So if you see albums and then you see something here, this is the code that I now want you to execute Or to try and put that a little bit simpler, what I really want to do is I really want to be able to explain to Python what the patterns are going to look like, and again, that's where routing comes into play So when we see that, let's go ahead and call that Details method, because at the end of the day, that's what our Python is going to call

Okay, now I'm going to leave that slide up there for about five more seconds, or apparently not There we go Sorry, Danny, I'm just going to throw you under the bus there There's the sort of propellerhead technical stuff >> That's a pretty scary looking slide

>> There it is Okay >> Good old root URL conf >> So let's go ahead and talk about how we're going to manage those patterns So patterns are going to be matched up by utilizing regular expressions

Now, if you haven't used regular expressions in a little while, that's what we're now going to introduce So Django uses those regular expressions for those patterns, and a regular expression, sometimes known by the way assessed a Perl regular expression same thing

is a way of describing a pattern So we know we're going to have that /albums/something afterwards And that's what a regular expression allows me to do, is it allows me to say, hey, the text is going to look like this

And we can actually get really complicated with this We can get very simple Maybe it's just a specific word We can say, hey, we only want numbers, or we can get something like, hey, we're going to have three letters followed by two numbers >> Right, so if we're expecting like an account number, we might say it should only be numbers, or if we're expecting a name, then we might only say letters, or if there's certain specific formats we expect, we can be really specific about what's an acceptable URL

>> Exactly So the basics of a regular expression, now, I will always remember, there was an O'Reilly book on regular expressions, and it was roundabout that thick I want to say literally this thing was like 700, 800 pages on regular expressions Our modules on MVA typically are around 50 minutes, and I'm looking at my clock right now, and it says that I've got 37 minutes and 39 seconds to get through the remainder of not only just regular expressions but a couple of other things So needless to say

why is it that people say needless to say and say it anyway? Needless to say, and I'm going to say it anyway, we're not going to get into everything about regular expressions Now, the good news about regular expressions is there's this old 80/20 rule that 80% of the time you use 20% of the features I think with regular expressions, it's basically a 95/5 rule, that 95% of the time, you use about 5% of the features

And so that's what I'm going to focus my attention on, is really just kind of those core little things that you want to be able to know, and then hopefully point you out a couple of resources from there So let's get into the basics >> I'll just add one thing >> Yeah >> I'm the lazy coder

When it comes to regular expressions, a lot of times, it'll be things like email addresses, postal codes, phone numbers and things you're trying to represent Usually, if you go and do a quick search on the web >> Don't steal my thunder >> Oh, am I stealing your thunder? >> You're stealing my thunder >> Yeah, trust me, there's some stuff out there that's going to help you

You don't need to memorize everything about regular expressions There are things that will help you, and Christopher may have some ideas on what >> Hey, hey So in any event, let's go ahead and kind of get rolling on working with our patterns here So the first thing, if we're going to be working with a regular expression and trying to match some pattern, we may need to be able to say, well, I know it's going to start with this or it's going to end with this

And that's where these two little characters come into play, the caret and the dollar sign, that the caret means that it has to start with whatever we're going to be providing, and then the dollar sign means that it must end with whatever it is that we're going to be providing So the caret is start, and then the dollar means that it's going to end with that So, for example, let's go ahead and take kind of a very simple little thing So here's my expression, and so my expression is simply the letters contactus, so simply contact, and that's it That's my expression

In regular expressions, if you just type out certain letters, what you're saying is those are the letters that I need to see So if I just type out the letters C-O-N-C-A-C-T, that's all that needs to be there I'm just saying, hey, I need to see those letters Now, you'll notice, if we go back to the expression, that there's no special characters here There's nothing in front of that and nothing at the end of it

So what this means is that somewhere inside of the text needs to exist in order the letters C-O-N-T-A-C-T And you'll notice that if we take a look, if this is our text contactus, contact exists, and so this is a match So it doesn't contain that dollar or that caret, so it matches, because contact wasn't anchored, meaning that it could be anywhere inside the word Let's change things a little bit How about if we put a dollar sign at the beginning there

So we've got our dollar sign at the very beginning, so what that means is it has to start with those letters We didn't say anything about the end, but it has to start with those letters So once again, if we go ahead and we take a look, you'll notice that sure enough, it starts with those letters, so we're good Well, what would happen if we put a caret at the end? Well, now what we're doing is we're saying it has to end with those letters, and if we take a look, of course, at our contactus, it ends with the letters us That again does not match

So if we have the caret, it means that it has to end with contact, so this does not match, because it does not end with those letters And if we kind of keep on going, we've got a $us Of course, that's not going to match, because it doesn't start with us Now, if I had left off that dollar, that would were Or if I had said us and then a caret, that would work

>> Because then it ends with the us letters >> Exactly But because of the fact that we had the dollar at the beginning, it doesn't start with US, so it's not going to match And in fact, I do have the slide on the US with the caret If we've got that, then it will in fact match

And just to close the entire thing out, you'll notice that you can say, well, I want it to start and end with those letters by using the dollar and the caret, and again, it doesn't match because we still have that us at the end Cool, all right And there's with the full contactus I've got a lot of examples in my slides here >> But it's one of those ones where when you see them after a while, you start to recognize the patterns and it becomes easier to work with it once you've seen a few examples

It really is the more you see them, the better you get at reading them >> Yeah, one of the things that people will often ask when they first see regular expressions is just like Susan pointed out there, is how in the world am I ever going to understand all of this? And the answer really is, it's all about just practice That regular expressions I think have slightly more interesting that's the word that I'm going to use, syntax than something like Python But when you first learn Python, you might have been looking at it and you might have been going, I really don't understand what in the world def means and what in the world class means But then after a little while and using it and so forth, you started to become a lot more comfortable with that, and now you really don't even think twice about it And regular expressions are basically the same thing, is that you're probably going to look at this the first time, especially if you're brand new to regular expressions, and you're just going to go, I don't quite get this If you keep working at it, eventually, it will start to all come together

Now, in the prior examples that we did, we were looking for a specific word, so I was specifically looking for contact, or I was specifically looking for contactus, but how about something that's truly a pattern So maybe we've got something that's a SKU or we've got something that's a phone number, and so what I need to make sure of is that we're looking for something like a phone number, so like for a US phone number, we're going to have, of course, the three digits for the area code, three digits, a dash, and then four more digits So how do I tell through regular expression that this is now what I want? So how about a true pattern? Well, what you're going to notice is that regular expressions actually gives us a lot of different ways that we can express a pattern So far starters, you're going to notice the backslash, and the backslash is a very common escape pattern, and that's what it is in regular expressions, but a d, so small d, means digit Make sense? >> Yes

>> And a D means non-digit And this is one little common pattern throughout regular expressions, is the lowercase letter means whatever it is that it's referring to The capital letter means the opposite, so a lowercase d means digit Capital d means digit Capital D means non-digit, and if we keep on keeping on, lowercase w means a word character

Capital W means a non-word character Now, that's what the w stands for W is a word character, so what it means is an alphanumeric and an underscore, so that counts A to Z, both upper and lowercase I didn't put the lowercase, just to keep the slide a little more compact, but it is A to Z, both upper and lowercase It's the underscore, but it's also 0 through 9, as well

And that can be a little bit problematic, because there are going to be times when I want characters, I just want A to Z, but I don't want numbers, and maybe I don't want underscores So how do I just make sure that I'm getting the letters A to Z and nothing else? Well, fortunately, you can actually create your own ranges And you do that by using square brackets Now, the thing about the way that the square brackets work, because it's a little bit funny when you first see it, is a square bracket, then some starting letter let's just say A through, I don't know, R for example, if you look at that, that sort of makes sense, because I've got my square bracket, and as long as we just simply expect that that's a special character, and that's what it's going to tell it, if you saw if I just simply said, hey, we need letters a to r, that would probably be how I'd write it, right down there

So when we see this up here, that kind of makes sense So we go, oh, okay, it's got to be somewhere between a and r That's fantastic Where things I think can get a little bit confusing is what happens if I just did this? And if you first look at that, you might think, well, that seems like it might just be a shortcut way to say, once again, a to z But it's not

If you list the letters a to z, but it's not If you list the letters off directly one after the other after the other, like I did there, just a to z, what you've now said is that you can choose a or you can choose z All the other letters, b through y, trying to remember my alphabet, b through y would not work there So I said you could use a or you could use z, but nothing in between Now, what happens, kind of going back to our word character, if you wanted just letters? Well, you can do that, but you just have to do that quote-unquote manually

So what you wind up doing is simply saying a to z, and then a to z, and you'll notice that I've just simply put both of those together just like that, so now it's a to z lower and a to z capital, and that will now give you all of the letters of the alphabet, including z, for my Canadian friends And by the way, if anybody's really curious, could I have done capital A, capital Z, and then lower az Absolutely, it doesn't matter which order you list them in >> You're just saying, I've got two ranges of letters, the uppercase range and the lowercase range >> That's exactly it

Yes, okay So if you want one letter, like a to z, or if you want one digit, then all you have to do is just simply use the appropriate icon and away you go So you want one digit, it's d What happens if you want two digits? Well, you could do that one of two ways You could, if you wanted to, say dd, which would work fine I guess for two digits, but what happens if you want 10 digits? >> It starts to get hard to keep track of how many of them you've done

When you find yourself counting them, then you probably want to switch syntax >> Exactly So the proper way, really, to do that is to use curly braces And inside the curly brace, you're going to indicate the number of items that you want Or if you want to create a range, so maybe I need between, say, three to six numbers

Then, you could just simply use curly and then two digits So 1,2, which would mean one or two, and then you notice that you just specify the maximum and minimum Now, where things get potentially a little bit confusing, and I've got a slide coming up that will talk about this in just a moment, is this operator applies to what immediately precedes it, not to the entire range, and I'll get to that in just a moment Now, how about if I just want one or more? That's a plus If I want zero or more, that's an asterisk, and if I just want to mark something as optional, then I use a question mark, and what's nice about things like the plus and the asterisk and the question mark is that those are very common in a lot of other places, as well, if you're going to be doing some form of query, and you're going to say I want one or more or zero or more, the plus, asterisk and question mark are kind of common

>> And let's be clear You're not going to memorize all of these from these slides in the time we're presenting them here You're going to look these up when you need them You're going to have a pattern you're looking for, and then you're going to go, oh, how do I search in a regular expression for a range of letters, range of numbers, one or more numbers, zero or more numbers, and you're going to look these up when you need them This is just trying to show you the pure flexibility in a few examples

>> Right So let's get a little bit more advanced, a little bit more advanced So like I mentioned, the number of modifiers only apply to the item that comes immediately before it So let's take, for example, that expression there, so I just simply have asdf, because my fingers were on the home row, and then I've got d and then that {2} This little operator that says 2 only applies to the item that comes immediately before it

So in other words, asdf42 matches, because it's two digits But if I said asdf42asdf42, that does not match, because the 2 does not apply to the entire thing Here we go Well, sort of There we go

It doesn't apply to the entire thing, but rather, it only applies to what immediately precedes it, so it's that d that immediately precedes it, so it's only talking about digits If you did in fact want to group, then you could in fact put that inside of parentheses Okay Now, I've got a couple more examples here that I'm going to show off in just a second, but what I want to mention before I get into that is kind of again, one more time, that the goal of this is not to get into everything there is to know about regular expressions Again, that would take a lot longer, and I've been sort of talking now

I'm trying to remember back to what the time said originally >> About 15 minutes >> Yeah, about 15 minutes I've been talking about regular expressions, and I've still got a couple more minutes to go Nobody's going to be able to learn regular expressions in 20 minutes But hopefully, this will help point you down the right path and that you'll start to at least see what the most common use cases are going to wind up being

So let's go back to our little example here, where I've got a bit of text over here, so album/Wish, so you'll notice that I'm sort of using more of a URL pattern And what you'll notice is that I've got my expression over here, and you'll notice that there's the expression, so I've got the dollar and I've got the caret, meaning that it has to start and end with that I've got the hardcoded letters album/, and then right here, I've got that + Now, you'll notice that this is red, and red is of course a nice indicator that this doesn't match The reason that it doesn't match is because it's case sensitive

So you'll notice that I've got Wish with a capital W You'll notice that I said lowercase a to z So as a result, this does not match Okay, well, what if I did want it to match? Well, I could go ahead and maybe write an expression that looks like that, but again, you're going to notice red Why is it red? Well, the reason that it's red is because I said AZaz

I didn't put in the dashes So AZaz is just going to be those four letters and only those four letters So it's not going to be that range Now, the reason that I started with those two as my examples, is because these are going to be very common mistakes So you might be thinking, well, it's case insensitive, A through Z, Bob's your uncle, not so much

You might be think, well, let's just go AZaz Again, not so much So the proper way to do it, the proper way to do it is to go ahead and type out A-Za-z, and so now this does in fact match Cool All right

Something else that you could also have done is you could have done the w, which again does mean word characters, but the problem there is remember what you're now saying So if I kicked all the way back to about like eight slides ago, one of the things that we would see is my word character not only includes a to z but it also includes numbers and it also includes underscores So if I'm talking about the name of an album, underscores, probably not Maybe numbers, maybe numbers, but underscores, probably not And so as a result, you really need to make sure that you're very careful when building your expressions so that they're properly going to limit what the user has typed in

Now, I do have to confess real quick, Susan, that if we were going to make this a real music store and we wanted to make this for artist, that particular artist from Minnesota, who for a little while had that unpronounceable symbol, I don't know how we would write a regular expression for that >> That would yeah, exactly

You never know what's going to happen in this world >> Yeah, exactly All right, in any event, that joke was much funnier in my head So how do we learn more about regular expressions? Well, honestly, it's all over the interwebs Like Susan mentioned earlier, just fire up Bing, do a search for regular expressions, and you'll find all sorts of different items

Within about like, I think, 30 seconds, I found diveintopython3net/regular-expressionshtml, so I go ahead and check that out Any tips when creating them? There's a great little website, and it's a community website called regexlibcom, and what you'll actually find on there is all sorts of different regular expressions, so if you're looking for a phone number, if you're looking for a zip code or a postal code, if you're looking for an email address, just head on over there, type whatever it is that you're looking for, and chances are, you're going to find it

There's a buddy of mine that likes to say, we're not launching rockets here The point that he's making is that whatever it is that we're doing, somebody else has already done, so go ahead and just take advantage of something that somebody else built I already mentioned the fact that you want to make sure that you restricted the pattern enough Now, the last thing specific to Django is this, is that Django is going to choose the first pattern that matches, so let's say that I wrote a pattern that looked like this, that I said, $/album and left it like that, and then I wrote another one that said $/album/, and let's say w+, there we go So let's say that I wrote those two patterns

This would actually pose a problem for me And the problem is, first of all, that I should have used a caret Let's try that again Give me one second See, we're really cooking on this show

There we go Caret, and now let's go ahead and say albums like that, and then let's say I also said caret, and I said albums/w+, so here's what these two expressions would mean So the first expression says, hey, it has to start with /albums/, and the second one says, well, it has to start with /albums/, and after that, there needs to be word characters The problem that I have is that this regular expression is going to match everything that the next regular expression is going to match, because after all, I said it just simply has to start with it I didn't say what would come after it

So it's not going to be based on what matches the closest It's whichever one matches first So you do have to be very careful when you get into configuring your routing How about that for a segue? >> Well done, sir >> Thank you, thank you

Okay, so let's get in and actually do it How do we actually add in our rules and our routes? Well, what you're going to do is you're going to list all of your expressions inside of urlspy Your default Visual Studio expression is going to have a whole set that you could go ahead and use, and here's the basic syntax The basic syntax is whatever your regular expression is, whatever the destination method name is that you want it to call, the actual code, and then you could also give it a name if you so desire

Or to break this down a little bit further, that r, which has to be there, tells Python that a regular expression is coming Then we have the regular expression that we want, and then the destination method is the one that we want it to match, and then like we said, a name if we so desire, which can be very helpful for testing purposes later What happens if it has parameters? Well, we'll actually kind of come back to that in just a moment, but we will be able to also add in parameters It's just going to be a little bit of syntax for that Okay, but first, let's go ahead and take a look at a basic URL

Now, when I'm doing my URLs, I generally like to just throw mine up at the top That's just me But again, you want to make sure that you are listing them in such a way that it's not going to accidentally match a later one, so you do need to be a little bit careful of that So all that we need to do is simply say url(4 for regular expression, single quote to start up a string, and then let's go ahead and create something for artists So let's say that if you type in artists, that I just want to display all of the artists, and I'm going to go ahead and put the dollar sign at the end there

I just want artists That's it, and so we'll go ahead and just do that And now it's going to be, well, where do we want to code? What code do we want to go run? Well, all of our code, and we talked about this in the morning, for our views is going to be, typically, inside of viewspy, or at least for a very, very small application You can create multiple files if you want

And so here is my little viewspy file right here, and I'm going to actually clean up a couple of things I was making sure everything was going to work earlier, so I added in a couple of things that I don't actually need right now, so I'm just cleaning those up There we go Okay, so there's our views

py file, so this is actually where our code is going to go So you'll notice right here that there's a method named home, right? Well, let's come back over here to urlspy, and just to get rid of the squigglies, if we take a look, that if you type in nothing after the domain, well, let's see it's inside of a folder called app, okay, okay, and views, well, it's inside of a file called views, we just identified that And then you're going to notice that there's the name of the method, right? Home So that's the method that it's now going to call

So what I'm eventually going to do here is I'm eventually going to create inside of appviews, a new method, then I'm going to call artists, and that's what's going to display all of my artists And let's give this name of artists There we go Now, don't forget the comma at the end

Now, one very nice thing about creating all of this is that you're actually creating a collection of patterns, and you'll notice that you could actually, if you wanted to, end with a comma So if you decide, hey, you know what, I really want all of mine down here at the end, and you're not sure about accidentally putting in one too many commas, that's okay So every time that you add in a new pattern, just get yourself into that habit, always put in that comma at the end There we go So there is for all of my artists

Now, one of the other things that we might want to be able to do here is we might want to be able to hand in a parameter So maybe I want to be able to display an artist or an album based on their name, or maybe based on their ID or whatever it is that it happens to be Well, in that particular case, I need to add on a parameter for that route, and the way that we add on a parameter, and again, I think it's a little bit of quirky syntax, but there it is, is we're going to inside of our route add in parentheses We're going to kind of create a group, question mark Actually, ?P is what's going to tell it that it's a parameter

This is eventually going to become the name of the parameter, and this is going to be the pattern So if I wanted to be able to say, for example, album, Wish, there we go If I want to be able to say album/Wish and have Wish be passed into that method that we just talked about, this is what I would wind up doing So this is the expression that's going to match that, and whatever the value is is going to wind up inside of name So if we kind of take a real quick look at that, let's come back over here to views, and hopefully, this will bring it all together

Let me say def artistdetails like this, and let's say, for example, our request and then name, so you'll notice that I'm just adding on a name I really don't care what it's going to return So anyway, let's just go ahead and add that in So request, as we're going to see in a couple of minutes, that's going to be the HTTP request object, so what the user has actually sent up There's my parameter name, so that's what it needs to match

So I'm going to come back over here to URLs, and I'm now going to say URL, r, just like before, and then we'll go ahead and say caret I can never find that on the keyboard Artists/, and then in parentheses, ?P name, again, matching name, and then what I want my pattern to be, so let's say A to Z, a to z, and let's add on a plus sign, paren, and let's close it out, and that's now going to go to app

views, and what did I call it, artistdetails? I have a great memory, it's just really short Thank you for laughing at that, Danny There we go And so now, if I just type in artists, that's going to give me all my artists And if I type in artists/, and I put something else at the end of it, that's now going to call that artistdetails method

Okay, now, you're probably looking at all of that, and you're probably thinking, okay, I sort of get it So I see that if the URL looks like this, I can tell it to point to that method, and if I see the URL looks like this, I can tell it to point to that method, but you know what I would really like to see? I would really like to see that in action I would really like to see, well, it call the method I want to see something display on the screen This is a web development class

Show me something in a browser Okay, fine We'll show you something in a browser So let's go ahead and execute code for the user So again, at the end of the day, the user is simply calling a method, so as a result, all we need to do is create a method for the user to call, and we actually just covered what that method needs to look like, so it's going to be the name, has to be a request object, and that again is going to be the request object that's coming back from the user, so this is what the user actually sent up via the browser, and then those optional parameters

So we can have name inside of there, or ID, or whatever else it is that we might want that we just saw And then, in order to send something back, the simplest way to do it is just to send back an HTTP response And so you could actually just build up the full HTML, and away you go, or if you wanted to send back a not found, then you could even send back a not found error message So let's go ahead and actually send that in, shall we? Let's actually see something displayed out So what I'm going to do is I'm going to come over here to my code, and let's go ahead and add in artists, so just to kind of highlight that once again, there's our artists, and we're going to call app

viewsartists, right? So I'm going to come back over here, and I'm going to go ahead and say defartists, and I'm going to say request, and let's go ahead and say return, and we want that HTTP response It's not imported by default, so I just need to add that in, HttpResponse, and let's go ahead and say return HttpResponse, HTML, head, title, Hello, Django We have to do Hello Django, right? >> It took us until Module 4 to get around to doing our Hello World

I can't believe it >> I blame myself H1, Hello, Django Close H1, close body and close HTML Okay

>> If you got all that right in the first try, I'm going to be very impressed >> Thank you All right, there we go And let's go ahead and do something like this I'm going to sort of cheat a little bit here

There we go And let's go ahead and say output equals, there we go >> So we're putting it all together now We've defined in the routing how do we know which URL calls which method, and now we're defining within the methods what to display to the user when those methods are called >> Exactly, yes

And I'm just kind of building this up across a couple of lines there So what I'm actually doing on this little method here is I am going to take whatever name it is that you provided and echo that back out to the screen That's all that I'm doing there So you give me the name, and I'm just going to display that back out, and again, I'm building up this HTML dynamically Not really the best way to build webpages, obviously, but it's going to be perfect for our little proof of concept

Okay Now, I'm going to build all of this, just click build and build solution And I could, if I wanted to, hit play, and that would launch the debugger, and that would launch Internet Explorer and all that good stuff But the way I like to do my development is I like to actually just right-click on the project, go to Python and then choose start server, or if you did want to attach a debugger, you do have a start debug server But I'm just going to start server

And this will actually automatically set up a little server here for me on come here Mark

There we go 1270

01, it's a local host, port 8000 And then what I could do is just navigate on over, and voila, there is my brand-new Django site Now, what I love about this is that server is going to be dynamic, so if I make a change to a py file and I save it, I come back here, I hit refresh, it works

It runs that code, so I love that back and forth, that the changes that I make are available to me almost instantaneously I really like that way of doing development, because after all, chances are, you probably want to be able to tweak a little bit, let's see if it works Tweak a little bit, let's see if it works It's generally speaking a good way to do development, and this supports that So I said artists, so let's say artists, and you'll notice that I kind of was making sure that everything was going to work earlier, so let's go in and say artists and

>> That was too much code to do in one shot without having something go wrong There's going to be an uppercase that should be a lowercase or something simple like that Artists plural, which is good, calling app

viewsartists Name equals artists You started your regular expression, starting with is the caret You did do artists plural, right? >> Oh, there we go

>> Oh, you have a slash on it >> This is so subtle Yeah, I had a slash in there, and that was what threw it off, yeah, so I had the slash in there So my code was right I typed in the wrong URL, so I had the slash in there, and I could go back and update my URL so that it would accept the slash

I'm not going to worry about that for right now, but that was what went wrong I just typed in my URL incorrectly, but you'll notice, sure enough, there's Hello, Django So now, let's go in and pass in a name here, so let's go ahead and zoom on in You'll notice that I've typed in the word name, and so now let's go ahead and try that You'll notice that it displays name

Let's go ahead and say Hello, Django, hit enter, and it doesn't like my spaces >> I was going to say, I suspect it doesn't like the spaces >> Yeah, because the reason is that that wasn't part of my pattern My pattern didn't say to allow spaces So what I'm going to do is just get rid of my spaces, HelloDjango, and now you'll notice that it is HelloDjango

So it is in fact executing that pattern there, and since I didn't include spaces, it's not going to put spaces in there I could include spaces if I wanted to, a s is whitespace That would work If I wanted underscores, I could put an underscore in there, and you start to see where all that is going But the real takeaway there is the fact I can set up my routing

I get to control what the URLs are going to look like, and if I want to be able to very easily read something from that URL and capture it inside of my view code, I have that ability >> Could you just take a minute and just go back to that original URL that you typed in, with the Hello, Django message, and show which URL it matched, sort of flow through the routing and then go to the method? Just quickly follow that code through one more time >> So here's the way that it winds up coming together, is it's going to read through all of my patterns, and actually, you know what I'm going to do? I'm going to do this So here's my little error message, and so you'll notice that it actually tells us exactly what it wound up doing behind the scenes So it says using the URLconf defined in music store URLs, Django tried these URL patterns in this order

So it tried looking for just the word artists Well, we didn't use just the word artists We have a forward slash there, so that is not a match Then it said, all right, well let me try with forward slash artists, and then that parameter, this is the important part right here of that parameter, it needs to have letters afterwards It needs to have letters afterwards

And so it's going to fail, because I have a forward slash, but I have no letters there, so it doesn't match there So that's the order in which it's being applied So the first one is going to match, if I just do artists, and then the second one is going to match if I do artists and then Hello, Django, or some bit of text afterward, so that's how it's all walking through there >> Cool >> Cool

All right, so with that, let's go ahead and take 10, and we'll come on back, and we'll take a look at how we could do some more advanced HTML and also how we could start to allow for changes to be made to our data >> See you soon >> Apparently