Python Django Tutorial 2018 for Beginners Part 3 - How to Create Views in Django + Templates

**Working with URLs in Django**

In this tutorial, we'll explore how to work with URLs in Django. We'll start by looping over each question and extracting its ID using the `question.dot_user_dot_notation` method.

To get the specific question's ID, we can use the dot notation, passing the question object as an argument. For example, `question.question_id`. This will give us the ID of the current question. We'll also be able to use the `_id` keyword in our URLs to achieve this, but for now, we'll stick with the dot notation.

As we loop over each question, we can generate a link to its detailed view using the `question.question_id`. The text that we're generating is simply `question.question_text`.

To change the URL pattern, we can use a dynamic way by replacing hard-coded values with variables. For example, `pole/specific/12` becomes `polls/specific/12`. This allows us to customize our URLs without having to hard-code them.

**Avoiding Collisions in Our Templates**

As we develop more apps within our Django project, it's essential to avoid collisions between their templates. To achieve this, we create a separate folder called `templates` for each app. If we want to add templates for a new app, say `blog`, we'll have an `app` called `blog` under the `templates` folder.

For example, in our current project, we have a `polls` app with a template called `index.html`. We can change this to use the dynamic URL pattern by replacing `polls` with `polls:detail` in the `URLs.py` file. This way, when we want to access the detailed view of a question, Django will understand which app it belongs to.

**Creating Separate Apps and Their URLs**

Now that we've learned about name spacing our URLs, let's create separate apps within our project. We'll add a new app called `blog`, and then create its templates folder. Inside this folder, we'll have another `index.html` file with the same content as our current polls template.

To avoid collisions, we need to make sure that each app has its unique URL pattern. For example, if we want to add a detail view for the blog post, we can use the dynamic URL pattern `blog:detail`. This way, when we access the detailed view of a blog post, Django will understand which app it belongs to.

**Conclusion**

In this tutorial, we've learned how to work with URLs in Django. We've explored how to loop over each question and extract its ID using the dot notation. We've also learned about name spacing our URLs to avoid collisions between different apps. Additionally, we've discussed how to create separate apps within our project and customize their URL patterns.

**The Profitable Programmer Course**

As a bonus, if you're interested in becoming a web developer, entrepreneur, or Python expert, I recommend checking out our flagship course called the Profitable Programmer. This comprehensive course will guide you through everything you need to know about Django, including creating apps, hosting, and getting clients. We'll also show you how to earn an income as a web developer and become a proficient programmer.

As a special bonus for joining our VIP waitlist, we'll give you access to your Python earning potential calculator. This tool will help you understand how much money you can make as a Python developer and set achievable goals for yourself. Don't miss out on this opportunity to transform your career!

To join our VIP waitlist and get instant access to the Profitable Programmer course, click the link below:

"WEBVTTKind: captionsLanguage: enyou welcome back to the most beautiful amazing jenga tutorial you've ever seen in your life let's get started we left off at part two we're gonna jump into part three and we're gonna get started so if you haven't already open up Adam go to where you created your mind site project right for me it was in github my site I'm gonna click open and I'm gonna say whatever open and recover state cool and here it is and then if I do command shift P and type in terminal my terminal pops up bada BAM and also make sure to do source activate my site and we are good okay so writing your first Django app part three so what they're talking about here is they're saying okay like look you have everything in Django is essentially called a view okay so for example if you have a blog homepage that's a view if you have the ability to comment like a comment action right that's considered a view if you click on a blog post and it shows you its details we refer to that as a detail page but that's still part of the view that's essentially what they're saying right over here another example of this is like let's say I take you to Instagram so let's say I click on clever cause II right this is essentially for Instagram and let me just plug my Instagram here to go follow me if you aren't already because awesome and if you go here right like look this is the home page for all my posts okay this would be considered a home page view if I click into it that's considered a detailed view because it's only showing that one particular post okay if I click here that's a comment action which is you know you can work that into the views so just wanted to show you that so you understand where all of this is coming from now we come over here and that's now they're saying that in our poll application we're essentially going to have index page in our views so this is gonna display the latest few questions we're going to have a detail page which is gonna display a text with no results but with a form to vote so when you click so like you come in you're a bunch of questions you click on a question and then boom it has like ability to vote it up and down okay I assume and we have a question results page so it's gonna display the result for a particular question how did that question do how many votes it got and then we have vote action so handles voting for a particular choice in a particular question okay cool all right so now we kind of know that we're gonna be creating like these things okay we're gonna be creating an index homepage of detail and results and also what they're staying over on this part is it saying like hey man have you ever seen really ugly URLs like this well don't worry because in Django you can make them really beautiful so you can have them like this instead like John Smith comm slash News Archive slash 2018 / - or / February whatever and you can make them look really nice how does it work it works off of URL conf s-- or your current URL configuration mapping okay so you map URL patterns to views so somebody goes to this URL it knows which view to run alright so now we're gonna write more views okay so we're gonna write these guys here again I'm just gonna copy it and talk about it for you I encourage you to type it all out and walk through it okay I'm just gonna highlight the main part so I'm gonna go in my poll slash views that py so in polls I'm gonna go inside of views or actually we're gonna leave the index for now paste it here okay so now we got detail we got results and we got vote deed everything always takes in a request object okay a request is passed whenever you do anything I'm now gonna touch on it too much right now but this is a first parameter you kind of always put in and then as your second parameter we're putting in the specific question ID this way we can look up that particular question from the database so let's say you wanted to look up a blog post right so you have multiple blog posts you want to be able to look up a specific one well we're gonna use ID for that and in this case our ID is our primary key and that will allow us to look up that unique thing or in the instagrams case it allowed us to look up that unique post okay cool now what we want to do is why are these new views into the polls dot URLs module by adding the following path calls okay so that's what it's showing here I'm gonna call I'm gonna copy this and I'm gonna paste it into our poll /url sup he Y I'm going to slash URLs and paste it here alright so what does this mean if you just go to slash polls right so John Smith dot-com slash polls it's gonna run this thing it's gonna match the MT pattern after polls and then it's gonna run the views out index function okay if you go to poll slash if you want to go to something like poll slash 5 or Instagram post slash whatever your post is right if you want to be able to do that you want a pattern that can match that okay so if I put in 20 it shouldn't break if I put in 8 it shouldn't break it should always be matched and so how we can do that is we basically do this thing with angle brackets and do int colon question underscore ID and this can dynamically match whatever pattern you put in another beautiful thing you guys usually for URL mapping you have to deal with ugly regular expressions except for the latest Jango you don't have to worry about regular expressions anymore so for example let's say you wanted to match this particular pattern well you can put this in and if somebody puts in a 5 here right it'll automatically know that it's an integer and work if they put in something else it might freak out okay so it's really smart and then it says hey if somebody goes to a URL like this automatically takes them to the detailed view and if somebody goes to a URL that ends in a results like this and which is essentially what you're saying here hey any number followed by results take them to the results 1 and any number followed by the word vote take them to the vote view cool and let's see what happens so I'm gonna run I'm gonna do Python manage that py run server cool and I will open up my Chrome and we will go to this guy over here okay so now we're getting an error is because we actually have to go to that particular URLs in this case let's go to slash polls and it should run our index function so I'm gonna do slash pulls boom it ran our index function what does our index function say let's go to our views and that's what it says hello world you have two polls index perfect well what if somebody goes to poll slash 5 or poll slash 193 what happens then it'll take us to the detail function and what does the detail function say it says you're looking at question number what the question ID cool so let's try and let's see what happens I'm gonna do 193 okay so the cool thing here that's happening is that you're able to take what's in the URL and you're able to pass it down to your HTML so right here that 193 right if I make it like some other crazy number it'll pass that down here right so now we're able to actually take in arguments from our URL and use them in our code what if I said something like this it'll say hey that pattern actually is not matched so that's exactly what we wanted if you don't put in a number here it should automatically detect it now if you want to go to something like slash poll slash a number slash results what do you do okay so let's say I have this number and I go to results just like that and now it should say something like you're looking at the results of question followed by whatever it is okay so you're looking at the results of question right or a question - and then if you go and try to do this thing with vote you're gonna get the same thing so if I go to my URLs it says hey go to slash poll slash number slash vote so we're gonna do slash poll slash number or slash votes and I hit enter and it says you're voting on question two perfect that's exactly what we should be getting here and it's looking great alright so that's saying hey like take a look at this in your browser and it'll display the placeholders and that's exactly what it did and how does it work well detail our function that we have will take in a request object followed by the question ID so when we pass in the question I need to be 193 right this part became 193 and this part is just that request object so what they have here again if you're confused about objects and what the hell's going on it's not completely necessary but you should look at some object-oriented programming stuff okay and I do have a course on object-oriented programming and you could comment on it if you're interested all right so the question ID is equal to 34 comes from this thing and I've explained that to you already when you put that in the URL this dynamically actually pulls it out and once it dynamically pulls it out because you see it says here it says question ID question ID that's where it's actually pulling it from okay and that's essentially it and then it says hey you don't need to do ugly things like latest HTML because it's not necessary and it's apparently silly okay so don't do it and you should write views that actually do something and here they're saying hey look each of you is responsible for doing one of two things either it should return what it's supposed to return or give you like a 404 and then the rest is up to you so you know basically you can have a view there it can read records from a database so meaning reading records reading post from a database Instagram post Facebook posts or it can generate PDF files or output XML or create a zip file on the fly anything you want and pretty much using whatever Python libraries you want cuz Django is 100 percent Python so you have full Python power and then all Django wants is that HTTP response okay so you can't return it as a string you can't return it you have to return it as some kind of an HTTP response or throw an exception okay so now here's what they're doing they're like alright we're gonna do something cool what we're gonna do is we're gonna take like all of the questions you have we're gonna order it by the publication date and then show the top five most recent once ok how are they doing that well I'm gonna copy this and then we're gonna play around with it ok so we're gonna go back to our thing and what do they have here from dot models import questions and they're in our poll slash views okay so this is one thing that we need to do from dot models import question because we don't have that so we're gonna paste it in here and then they want us to redefine our index okay so we're gonna do just that I'm gonna change my definition of what my index function is alright so how is this working well question dot question that objects are ordered by and what this thing does is it'll take all your questions and order it by something so in our models if you look we have this thing publication date so pub underscore date so we're gonna order it by a publication date now what we're doing with this minus sign is we're saying in the reverse order okay so instead of the oldest publication date and showing us then the oldest way we wanted to show it in a descending order so we wanted to show us the recent ones first and then what we're doing is that we're just and so this will return to us a list okay and then you can index a list in Python by doing this and we're just saying give us the FIR five so from 0 up to but not including 5 so 0 1 2 3 4 that's 5 ok and then as output we're saying hey join all of them all of the questions by a comma okay so if you want to know a little bit more about like how the Python is working in there so here we're doing a list comprehension and you can read more about list comprehensions if you don't know what that is it's not too important and it's just a cool way of writing this you know instead of multiple lines you just write in one line and we're just saying for each question give us its text that's all we're saying and then we're saying join it all by commas so that's how it's gonna put it so it's gonna return to us pretty much a string okay and then we want to return that okay as our output so we're just gonna do HTTP response output that's it so now let's take a look at it so I'm gonna save it and we're gonna go to our thing and we'll try to go to our index and where's our index it's just that slash pulls okay that should trigger our index and let's see what happens so I hit enter and it shows me all my questions if you remember I created three questions what's cracking what's new what's up and it's showing us what separated by a comma huh I could do show it to us separated by three stars if i refresh it separated by three stars you could separate it by an image whatever you want you know this is just pure Python okay so that's essentially it but there's something wrong here and the thing that's actually wrong here is like look you're not gonna have your toilet and your refrigerator in the same room right you're muddying the water just like that you don't want to have your HTML code and your Python in the same place we want to kind of separate it out so right now the design of our page is in the same place that handles our logic okay so what we want to do is handle our logic by your Python in one place and all our HTML and the design of the page should be outside of this logic okay so we want to create something for that so what we're gonna do and what this tutorial tells us to do is inside polls you want to create a new folder and you want to call this folder templates this is important the case sensitivity of this is important as well as the name so if you mess up the name when you put a typo you're gonna kind of get messed up here also pay attention to the order of Aldus okay so under my site so under polls you're gonna have templates and then inside of templates you want to create a new folder and you want to call it polls all right and inside of this is where you're gonna throw all your HTML files so we're gonna create we're gonna create a new file in here and we're gonna call it index.html okay so just in slow motion for you polls templates polls index okay so essentially it's like polls templates polls index like that alright let's go back to our tutorial and that's what it's saying it's saying that hey django will automatically look for it and find these templates and essentially to django the path will look like pull slash index dot HTML because of how django works and looks for these and you can override it and do all kinds of advanced stuff you know if you want to read more into that we're gonna just kind of keep it a little bit basic so everybody can follow alright and now we're gonna put the following code into into that template so it's telling us which file to put that code and so i'm just gonna hit this button copy this code and we're gonna go into our index.html and i will paste it right over here okay what is this code saying it's saying hey if there are any questions then i want you to create an unordered list okay that's what a UL tag is in HTML and then what I want you to say is for a question and so for any questions I want you to put it as like a bullet point and show that question okay and link to that particular question otherwise say that there are no polls available so if we didn't have any questions at all it'll send no polls available and then end the if statement okay so we're starting our for loop here we're ending it here we start our if statement here and then we have our else statement here and we have our end if here all right so that's cool now we want to make sure that we actually link to this index.html file how are we gonna do that here's how now let's update our index view in pulse slash views that py to use the template okay so we're gonna do just that and also let's update our index function just a little bit so here's a few lines that we're adding let's go back to our views and we will so lay this question we'll keep that and we'll just paste this in here alright save so what's going on we're still getting those most recent five questions but that's what latest question list is gonna become and if you don't have five questions they'll pick the top three or top or the recent most four okay something like that and then what we're doing is we're loading that template poll slash index at HTML okay so we're using loader and then there's something called context dictionary in Django and what you can do is you can pass from the back-end server side pass this to your front-end your HTML code it'll know about it okay so if I go back to our templates my index.html how is it getting access to this variable latest question list well we're actually passing it in our context alright that's that's what's going on and then we're saying as HTTP response template dot render were saying hey send that context with that request and send it over to the HTML file so it sends all that over to the HTML file and then our HTML file has access to that latest question ok and this weird stuff you're seeing here with this with a % squiggly bracket that's Django template in engine okay so it's basically HTML with the little Django pizazz cool and so now they're like hey this is such a common step to load a template and then to do template render that there they made a shortcut for it which is just render so how can you use that like this okay so basically you can remove this line you don't need this line anymore and thanks to this line at the top from Django da shortcuts import render which you should have we're gonna use this render and how are we gonna do it we can just replace all of this and we can say render the first argument is request okay and then after that the path to that index file so poles slash index dot HTML just like that followed by the context dictionary so in this case we're just calling it context okay sometimes what I like to call this is stuff for front-end alright just to keep it consistent and just so you know the thing that you're accessing on the back end is not this guy it's the dictionary context dictionary key so it's actually this string alright and that's will give you back this guy okay so note that once we've done all this in views we no longer need to import loader blah blah blah exactly that's what we did and now they're gonna show us how to raise a 404 error all right so what happens if it doesn't exist so let's tackle the question detail view the page that displays the question text for a given poll here's the view so in poll slash views that py what we're gonna do is we're gonna from Django DHCP import HTTP 404 so since we were already using Django DHCP to import HTTP response we're also gonna say HTTP 404 okay just like that we're gonna save it and then we're gonna come back and now it wants us to make changes to our detailed view and it's saying hey turn it into this so let's turn it into that and I'm gonna save it so try except blocks all it does is if there is an error try except blocks will catch it okay try except block it is pretty much the same thing as if then condition except that if you're running into some kind of crazy error try except blocks will catch it and still run for example if you divided a number by zero and Python your code will just freakin crash right they'll give you this red blob your app will crash whatever it but if you run a try except block that says if you get a zero division error then just pass you'll not get that error I'll just pass it okay so what we're saying is we're saying try to get that particular that one specific question okay so we're saying we're getting that question ID and we're saying get that object that has that primary key or that question ID or where that primary key is equal to the question ID so if I pass in a URL like um app comm slash polls slash six right what it's gonna do or let's say / - what it'll do is pass this two in to here that this will become - and the question will get the question object that has an ID of two in this case it might be one of my question that I think is what's new right now what's new question so then this question will become the what's new question object and then what we do is that we just return that okay and we pass it in our context dictionary and we pass that question in here except if the question does not exist then we raise HTTP four and we said question doesn't exist okay cool let me make sure to delete that that's not supposed to be there I just added that to show you guys what's up okay the new concept here the view raises yep we'll discuss what you could put in that poll slash detail that HTML template a bit later but if you'd like to quickly get the above example working a file containing just this okay so we're gonna get that thing working okay so in here we're gonna create a new file and we're gonna call it detail dot HTML and in detail we're just gonna put questions just like that okay so now we got to make it work and we're gonna go and get our question to okay so because that specific question exists look it says what's new now if I change this ID to one is gonna get the question that was what's up if I change it to three it's gonna be the what's cracking but what happens if I change it to four remember I only created three questions if I change it to four it says page not found and look the error it throws up this question does not exist now if we didn't have that right if we actually don't have that so let's go back to our views and I'll just remove this and I'll just say instead of raised I'll just say pass okay Pass is equivalent of saying ignore to Python let's go back let's refresh we're gonna get this ugly error which we don't actually know what to do with or what it means and it is very confusing but when we have this when we raise this thing in a refresh you see it says question does not exist so it's a lot easier for us to see what the problem is is because we know it's coming from here then we can like start debugging and know what's going on okay so that's why that particular thing is important all right let's move on along all right so a shortcut so instead of having this try accepting on rays we can actually do a cute shortcut that says get out or 404 and that's a method that's in Python in Django it comes with it so we'll throw that and it's in our shortcut so from Django - shortcuts import render and actually import get object or 404 okay and just to stay consistent with them we'll do it in this because it starts with the G and starts with R so alphabetical orders and Pepe tanned okay alright so we're gonna go here we will go to where it says question in detail and we're gonna remove this whole thing okay make our life easier and all we're gonna do is we're gonna say but BAM right we're gonna say hey get object or 404 so that particular question with the primary key of this if it doesn't exist then it'll automatically say it doesn't exist and then pretty much the last line we'll keep it what it was we'll save this and basically as our context dictionary we're saying passed a question as a question okay so there are three arguments in here request the HTM passes HTML file as a string and then a dictionary so then we can use it on our back-end again in our detail HTML we can actually use we can see that question save and now let's try to go to it again so I'll say for and now look it says no question matches the given query question as in like no question object okay but if I do three it finds us and takes us to it so this is what we mean this is what I mean when I say python is really a Django is really awesome it comes with a lot of intelligent defaults whereas you don't have to write allotted a lot of this code right it works on that dry principle do not repeat yourself and a lot of these things just come built-in out-of-the-box and it it speeds up your development time saves you a lot of those new code lines because every new code line that you write there's an additional chance for an error so the amount of code lines you can do use less errors you will make plus your code just looks beautiful and so much more readable right get object or 404 instead of try that it added up accept object does not exist right so it's confusing okay let's go on get object or 404 this is some advanced stuff we're gonna skip that for now now that says use the template system so in our detailed HTML we're gonna go and we're gonna paste that there save it and let's see what it's saying back to the detail view for our pol application here's what it should look like okay so now let's go to the detail and let's see first let's just see what the result is right and then we'll talk about it I'll hit enter what's cracking is showing it to me in a nicer way let's do two it's showing it to me in a nicer way what happens if I do four same thing no question matches the given query okay so what are they doing here did take your a particular question and they wrap it up in h1 tag in HTML anything that's an h1 tag will make it's called a heading one and you can go all the way down to heading six heading six being the smallest heading one being the biggest boldest so we have an in a heading one and then right underneath it we say four choice in question dot choice underscore set at all so for all of the choices as like at choices as in the particular answers you can have two each question right though those answers are the ones that actually get an upload or there's no down of those so an upload show me all of those answers except in this case we don't have any answers or any choices so that's why it's not showing those how is it working the template system uses dot lookup syntax so you'll do question doc question text so go into the question and then it'll access the question underscore text another thing to notice if you're more advanced if the question of the score text attribute did not exist it would go and try to access it as a index okay so imagine if it was like if you did question dot zero or something like that so you you're still using dot notation so that's something that could trip you up later okay it would have tried a list index lookup okay and then method calling happens in the for loop so this for loop method calling is happening there because you know you're doing question that choice underscore set dot all okay and then it's interpret as Python code which is cool like right here and it returns an iterable of choice objects and then we iterate over it right so that's why we use a for loop and then we iterate over it now remember when we wrote the link to a question the pole slash index dot HTML so let's go to our pole slash index dot HTML you can see what we did here right so we said slash so for the links we said slash polls slash this right so let's go take a look at this page one more time we're gonna go right here and I'm just gonna hit holes and hit enter so you can see how it's showing me what's cracking what's new what's up so each of this and when you click into it you go into its detailed view right kind of like when you click into a blog post and also under what's up you can actually see the choices so that's just a recent thing we just added right so how is that working how are we linking it and how we're linking and we're saying slash polls slash that question ID which matches one of the paths in our URLs that py so if I take you to my URL it'll match the path of this guy and that takes you to the detail view and that's how we can see the detailed view so let's go back but we're hardwiring the URL paths this way if we have a lot of URL paths our logic can get messed up or if we change if we go in our URLs up he why and I I don't know change this path to be something else right it has like it says polls here again followed by something else it could like mess us up so what we want to do is we want to use it in a dynamic way so that's what they're showing us here so they're saying hey replace this guy with this guy so we're gonna do just that okay so we're gonna go back to our index.html and we will just do this okay so now what this is saying is for the URL use detail detail as in this that's where we're getting the name from and then as the argument pass in the question ID so this question since we're looping over it if you do question dot user dot notation it'll get you that particular questions ID so question that ID that's what you pass in as the argument and so then when you go to your URLs to this question Aidid that's what it's gonna pass okay that's essentially it and I believe you should also be able to do question underscore ID equals that but in this case we're just gonna keep it like this okay so no need for keyword arguments okay and then in in between here so that's what its gonna link to it's gonna link to the detailed view of that particular question and the text that we're generating is just from right here so that's question and dot question underscore text if you are confused by this look up stuff on HTML okay and look up how links work in HTML again I'm not gonna go into too much HTML because that's outside the scope of this tutorial okay okay and that's how it's essentially working now namespacing URL names it's basically just telling us like hey look so let's go down over here let's just make sure we're not missing anything okay from the top okay so they're saying if you want to change the URL to something else perhaps is something like pole slash specific slash 12 so this is something that I've already said but but they're just reiterating here and they're saying it like if you change the URL it's still gonna work now that we change it to be the dynamic way okay so now we can change our URL pattern to whatever and it's not hardwired anymore and now they're talking about name spacing URL names okay so this way you want to make sure that all of your apps actually have there's no way for like right now we only have two poles app right but what if we wanted to have more apps inside of this then what could happen is that if any of our HTML files name matched so let's say you have a poles app and it has an index.html file right what if you had a blog app and that also had an index.html file now you're gonna have a collision so what we do for that that's us that's the reason why we create under templates another folder called poles and if we want it to add templates for a blogging app that's why we would have an app called blog and then under that we'd have templates and then under that we'd have blog and then under that would put our HTML stuff okay that's the reason why we do it like that and this way there's gonna be no collisions okay now change your poll slash index dot HTML so let's see what it's saying here so we can have our app name app underscore named Abner our name is equal to polls so this is in our URLs a py so let's go right there URLs not py and they're saying hey add that guy right over here okay and pretty much everything else we want to just leave as is now change your poll slash index dot HTML template from this to this so in our index dot HTML we're gonna go right here and we're gonna paste that guy so notice the difference all we did is we do polls equals D P polls colon detail okay that's really the only difference there's no other difference I'm gonna save that and this way our URLs will never collide and everything has a proper name spacing right because right now we created a detail for our polls what if you want to create a in what if you had to add a new app called blog and then create details in there you don't want it to just be called detail because it's gonna collide with the polls detail but now because it's polls : detailed then later you can do blog : detail or Instagram post : detail or Facebook status : detail and this way you can differentiate all of those different apps you have within one of your Django projects ok so hopefully that makes sense hopefully this tutorial was helpful for you that's it for this video I do got to let you know that we do have our flagship course called the profitable programmer coming up that's to help you become a web developer become a badass in Python and Django and everything to do with becoming a web developer as you know as a Python web developer and we also show you how to get clients and earn an income and much more in depth ok there we're also gonna go deep into Django and help you create apps and host so if you are interested in this please go to this link over here and join our VIP waitlist and as my bonus to you I'm gonna give to you your Python earning potential calculator and it's really cool I'm gonna give you that for free okay so go to that link right now and join the VIP waitlist but that's that that's it and I'm gonna see you in the next part of this tutorialyou welcome back to the most beautiful amazing jenga tutorial you've ever seen in your life let's get started we left off at part two we're gonna jump into part three and we're gonna get started so if you haven't already open up Adam go to where you created your mind site project right for me it was in github my site I'm gonna click open and I'm gonna say whatever open and recover state cool and here it is and then if I do command shift P and type in terminal my terminal pops up bada BAM and also make sure to do source activate my site and we are good okay so writing your first Django app part three so what they're talking about here is they're saying okay like look you have everything in Django is essentially called a view okay so for example if you have a blog homepage that's a view if you have the ability to comment like a comment action right that's considered a view if you click on a blog post and it shows you its details we refer to that as a detail page but that's still part of the view that's essentially what they're saying right over here another example of this is like let's say I take you to Instagram so let's say I click on clever cause II right this is essentially for Instagram and let me just plug my Instagram here to go follow me if you aren't already because awesome and if you go here right like look this is the home page for all my posts okay this would be considered a home page view if I click into it that's considered a detailed view because it's only showing that one particular post okay if I click here that's a comment action which is you know you can work that into the views so just wanted to show you that so you understand where all of this is coming from now we come over here and that's now they're saying that in our poll application we're essentially going to have index page in our views so this is gonna display the latest few questions we're going to have a detail page which is gonna display a text with no results but with a form to vote so when you click so like you come in you're a bunch of questions you click on a question and then boom it has like ability to vote it up and down okay I assume and we have a question results page so it's gonna display the result for a particular question how did that question do how many votes it got and then we have vote action so handles voting for a particular choice in a particular question okay cool all right so now we kind of know that we're gonna be creating like these things okay we're gonna be creating an index homepage of detail and results and also what they're staying over on this part is it saying like hey man have you ever seen really ugly URLs like this well don't worry because in Django you can make them really beautiful so you can have them like this instead like John Smith comm slash News Archive slash 2018 / - or / February whatever and you can make them look really nice how does it work it works off of URL conf s-- or your current URL configuration mapping okay so you map URL patterns to views so somebody goes to this URL it knows which view to run alright so now we're gonna write more views okay so we're gonna write these guys here again I'm just gonna copy it and talk about it for you I encourage you to type it all out and walk through it okay I'm just gonna highlight the main part so I'm gonna go in my poll slash views that py so in polls I'm gonna go inside of views or actually we're gonna leave the index for now paste it here okay so now we got detail we got results and we got vote deed everything always takes in a request object okay a request is passed whenever you do anything I'm now gonna touch on it too much right now but this is a first parameter you kind of always put in and then as your second parameter we're putting in the specific question ID this way we can look up that particular question from the database so let's say you wanted to look up a blog post right so you have multiple blog posts you want to be able to look up a specific one well we're gonna use ID for that and in this case our ID is our primary key and that will allow us to look up that unique thing or in the instagrams case it allowed us to look up that unique post okay cool now what we want to do is why are these new views into the polls dot URLs module by adding the following path calls okay so that's what it's showing here I'm gonna call I'm gonna copy this and I'm gonna paste it into our poll /url sup he Y I'm going to slash URLs and paste it here alright so what does this mean if you just go to slash polls right so John Smith dot-com slash polls it's gonna run this thing it's gonna match the MT pattern after polls and then it's gonna run the views out index function okay if you go to poll slash if you want to go to something like poll slash 5 or Instagram post slash whatever your post is right if you want to be able to do that you want a pattern that can match that okay so if I put in 20 it shouldn't break if I put in 8 it shouldn't break it should always be matched and so how we can do that is we basically do this thing with angle brackets and do int colon question underscore ID and this can dynamically match whatever pattern you put in another beautiful thing you guys usually for URL mapping you have to deal with ugly regular expressions except for the latest Jango you don't have to worry about regular expressions anymore so for example let's say you wanted to match this particular pattern well you can put this in and if somebody puts in a 5 here right it'll automatically know that it's an integer and work if they put in something else it might freak out okay so it's really smart and then it says hey if somebody goes to a URL like this automatically takes them to the detailed view and if somebody goes to a URL that ends in a results like this and which is essentially what you're saying here hey any number followed by results take them to the results 1 and any number followed by the word vote take them to the vote view cool and let's see what happens so I'm gonna run I'm gonna do Python manage that py run server cool and I will open up my Chrome and we will go to this guy over here okay so now we're getting an error is because we actually have to go to that particular URLs in this case let's go to slash polls and it should run our index function so I'm gonna do slash pulls boom it ran our index function what does our index function say let's go to our views and that's what it says hello world you have two polls index perfect well what if somebody goes to poll slash 5 or poll slash 193 what happens then it'll take us to the detail function and what does the detail function say it says you're looking at question number what the question ID cool so let's try and let's see what happens I'm gonna do 193 okay so the cool thing here that's happening is that you're able to take what's in the URL and you're able to pass it down to your HTML so right here that 193 right if I make it like some other crazy number it'll pass that down here right so now we're able to actually take in arguments from our URL and use them in our code what if I said something like this it'll say hey that pattern actually is not matched so that's exactly what we wanted if you don't put in a number here it should automatically detect it now if you want to go to something like slash poll slash a number slash results what do you do okay so let's say I have this number and I go to results just like that and now it should say something like you're looking at the results of question followed by whatever it is okay so you're looking at the results of question right or a question - and then if you go and try to do this thing with vote you're gonna get the same thing so if I go to my URLs it says hey go to slash poll slash number slash vote so we're gonna do slash poll slash number or slash votes and I hit enter and it says you're voting on question two perfect that's exactly what we should be getting here and it's looking great alright so that's saying hey like take a look at this in your browser and it'll display the placeholders and that's exactly what it did and how does it work well detail our function that we have will take in a request object followed by the question ID so when we pass in the question I need to be 193 right this part became 193 and this part is just that request object so what they have here again if you're confused about objects and what the hell's going on it's not completely necessary but you should look at some object-oriented programming stuff okay and I do have a course on object-oriented programming and you could comment on it if you're interested all right so the question ID is equal to 34 comes from this thing and I've explained that to you already when you put that in the URL this dynamically actually pulls it out and once it dynamically pulls it out because you see it says here it says question ID question ID that's where it's actually pulling it from okay and that's essentially it and then it says hey you don't need to do ugly things like latest HTML because it's not necessary and it's apparently silly okay so don't do it and you should write views that actually do something and here they're saying hey look each of you is responsible for doing one of two things either it should return what it's supposed to return or give you like a 404 and then the rest is up to you so you know basically you can have a view there it can read records from a database so meaning reading records reading post from a database Instagram post Facebook posts or it can generate PDF files or output XML or create a zip file on the fly anything you want and pretty much using whatever Python libraries you want cuz Django is 100 percent Python so you have full Python power and then all Django wants is that HTTP response okay so you can't return it as a string you can't return it you have to return it as some kind of an HTTP response or throw an exception okay so now here's what they're doing they're like alright we're gonna do something cool what we're gonna do is we're gonna take like all of the questions you have we're gonna order it by the publication date and then show the top five most recent once ok how are they doing that well I'm gonna copy this and then we're gonna play around with it ok so we're gonna go back to our thing and what do they have here from dot models import questions and they're in our poll slash views okay so this is one thing that we need to do from dot models import question because we don't have that so we're gonna paste it in here and then they want us to redefine our index okay so we're gonna do just that I'm gonna change my definition of what my index function is alright so how is this working well question dot question that objects are ordered by and what this thing does is it'll take all your questions and order it by something so in our models if you look we have this thing publication date so pub underscore date so we're gonna order it by a publication date now what we're doing with this minus sign is we're saying in the reverse order okay so instead of the oldest publication date and showing us then the oldest way we wanted to show it in a descending order so we wanted to show us the recent ones first and then what we're doing is that we're just and so this will return to us a list okay and then you can index a list in Python by doing this and we're just saying give us the FIR five so from 0 up to but not including 5 so 0 1 2 3 4 that's 5 ok and then as output we're saying hey join all of them all of the questions by a comma okay so if you want to know a little bit more about like how the Python is working in there so here we're doing a list comprehension and you can read more about list comprehensions if you don't know what that is it's not too important and it's just a cool way of writing this you know instead of multiple lines you just write in one line and we're just saying for each question give us its text that's all we're saying and then we're saying join it all by commas so that's how it's gonna put it so it's gonna return to us pretty much a string okay and then we want to return that okay as our output so we're just gonna do HTTP response output that's it so now let's take a look at it so I'm gonna save it and we're gonna go to our thing and we'll try to go to our index and where's our index it's just that slash pulls okay that should trigger our index and let's see what happens so I hit enter and it shows me all my questions if you remember I created three questions what's cracking what's new what's up and it's showing us what separated by a comma huh I could do show it to us separated by three stars if i refresh it separated by three stars you could separate it by an image whatever you want you know this is just pure Python okay so that's essentially it but there's something wrong here and the thing that's actually wrong here is like look you're not gonna have your toilet and your refrigerator in the same room right you're muddying the water just like that you don't want to have your HTML code and your Python in the same place we want to kind of separate it out so right now the design of our page is in the same place that handles our logic okay so what we want to do is handle our logic by your Python in one place and all our HTML and the design of the page should be outside of this logic okay so we want to create something for that so what we're gonna do and what this tutorial tells us to do is inside polls you want to create a new folder and you want to call this folder templates this is important the case sensitivity of this is important as well as the name so if you mess up the name when you put a typo you're gonna kind of get messed up here also pay attention to the order of Aldus okay so under my site so under polls you're gonna have templates and then inside of templates you want to create a new folder and you want to call it polls all right and inside of this is where you're gonna throw all your HTML files so we're gonna create we're gonna create a new file in here and we're gonna call it index.html okay so just in slow motion for you polls templates polls index okay so essentially it's like polls templates polls index like that alright let's go back to our tutorial and that's what it's saying it's saying that hey django will automatically look for it and find these templates and essentially to django the path will look like pull slash index dot HTML because of how django works and looks for these and you can override it and do all kinds of advanced stuff you know if you want to read more into that we're gonna just kind of keep it a little bit basic so everybody can follow alright and now we're gonna put the following code into into that template so it's telling us which file to put that code and so i'm just gonna hit this button copy this code and we're gonna go into our index.html and i will paste it right over here okay what is this code saying it's saying hey if there are any questions then i want you to create an unordered list okay that's what a UL tag is in HTML and then what I want you to say is for a question and so for any questions I want you to put it as like a bullet point and show that question okay and link to that particular question otherwise say that there are no polls available so if we didn't have any questions at all it'll send no polls available and then end the if statement okay so we're starting our for loop here we're ending it here we start our if statement here and then we have our else statement here and we have our end if here all right so that's cool now we want to make sure that we actually link to this index.html file how are we gonna do that here's how now let's update our index view in pulse slash views that py to use the template okay so we're gonna do just that and also let's update our index function just a little bit so here's a few lines that we're adding let's go back to our views and we will so lay this question we'll keep that and we'll just paste this in here alright save so what's going on we're still getting those most recent five questions but that's what latest question list is gonna become and if you don't have five questions they'll pick the top three or top or the recent most four okay something like that and then what we're doing is we're loading that template poll slash index at HTML okay so we're using loader and then there's something called context dictionary in Django and what you can do is you can pass from the back-end server side pass this to your front-end your HTML code it'll know about it okay so if I go back to our templates my index.html how is it getting access to this variable latest question list well we're actually passing it in our context alright that's that's what's going on and then we're saying as HTTP response template dot render were saying hey send that context with that request and send it over to the HTML file so it sends all that over to the HTML file and then our HTML file has access to that latest question ok and this weird stuff you're seeing here with this with a % squiggly bracket that's Django template in engine okay so it's basically HTML with the little Django pizazz cool and so now they're like hey this is such a common step to load a template and then to do template render that there they made a shortcut for it which is just render so how can you use that like this okay so basically you can remove this line you don't need this line anymore and thanks to this line at the top from Django da shortcuts import render which you should have we're gonna use this render and how are we gonna do it we can just replace all of this and we can say render the first argument is request okay and then after that the path to that index file so poles slash index dot HTML just like that followed by the context dictionary so in this case we're just calling it context okay sometimes what I like to call this is stuff for front-end alright just to keep it consistent and just so you know the thing that you're accessing on the back end is not this guy it's the dictionary context dictionary key so it's actually this string alright and that's will give you back this guy okay so note that once we've done all this in views we no longer need to import loader blah blah blah exactly that's what we did and now they're gonna show us how to raise a 404 error all right so what happens if it doesn't exist so let's tackle the question detail view the page that displays the question text for a given poll here's the view so in poll slash views that py what we're gonna do is we're gonna from Django DHCP import HTTP 404 so since we were already using Django DHCP to import HTTP response we're also gonna say HTTP 404 okay just like that we're gonna save it and then we're gonna come back and now it wants us to make changes to our detailed view and it's saying hey turn it into this so let's turn it into that and I'm gonna save it so try except blocks all it does is if there is an error try except blocks will catch it okay try except block it is pretty much the same thing as if then condition except that if you're running into some kind of crazy error try except blocks will catch it and still run for example if you divided a number by zero and Python your code will just freakin crash right they'll give you this red blob your app will crash whatever it but if you run a try except block that says if you get a zero division error then just pass you'll not get that error I'll just pass it okay so what we're saying is we're saying try to get that particular that one specific question okay so we're saying we're getting that question ID and we're saying get that object that has that primary key or that question ID or where that primary key is equal to the question ID so if I pass in a URL like um app comm slash polls slash six right what it's gonna do or let's say / - what it'll do is pass this two in to here that this will become - and the question will get the question object that has an ID of two in this case it might be one of my question that I think is what's new right now what's new question so then this question will become the what's new question object and then what we do is that we just return that okay and we pass it in our context dictionary and we pass that question in here except if the question does not exist then we raise HTTP four and we said question doesn't exist okay cool let me make sure to delete that that's not supposed to be there I just added that to show you guys what's up okay the new concept here the view raises yep we'll discuss what you could put in that poll slash detail that HTML template a bit later but if you'd like to quickly get the above example working a file containing just this okay so we're gonna get that thing working okay so in here we're gonna create a new file and we're gonna call it detail dot HTML and in detail we're just gonna put questions just like that okay so now we got to make it work and we're gonna go and get our question to okay so because that specific question exists look it says what's new now if I change this ID to one is gonna get the question that was what's up if I change it to three it's gonna be the what's cracking but what happens if I change it to four remember I only created three questions if I change it to four it says page not found and look the error it throws up this question does not exist now if we didn't have that right if we actually don't have that so let's go back to our views and I'll just remove this and I'll just say instead of raised I'll just say pass okay Pass is equivalent of saying ignore to Python let's go back let's refresh we're gonna get this ugly error which we don't actually know what to do with or what it means and it is very confusing but when we have this when we raise this thing in a refresh you see it says question does not exist so it's a lot easier for us to see what the problem is is because we know it's coming from here then we can like start debugging and know what's going on okay so that's why that particular thing is important all right let's move on along all right so a shortcut so instead of having this try accepting on rays we can actually do a cute shortcut that says get out or 404 and that's a method that's in Python in Django it comes with it so we'll throw that and it's in our shortcut so from Django - shortcuts import render and actually import get object or 404 okay and just to stay consistent with them we'll do it in this because it starts with the G and starts with R so alphabetical orders and Pepe tanned okay alright so we're gonna go here we will go to where it says question in detail and we're gonna remove this whole thing okay make our life easier and all we're gonna do is we're gonna say but BAM right we're gonna say hey get object or 404 so that particular question with the primary key of this if it doesn't exist then it'll automatically say it doesn't exist and then pretty much the last line we'll keep it what it was we'll save this and basically as our context dictionary we're saying passed a question as a question okay so there are three arguments in here request the HTM passes HTML file as a string and then a dictionary so then we can use it on our back-end again in our detail HTML we can actually use we can see that question save and now let's try to go to it again so I'll say for and now look it says no question matches the given query question as in like no question object okay but if I do three it finds us and takes us to it so this is what we mean this is what I mean when I say python is really a Django is really awesome it comes with a lot of intelligent defaults whereas you don't have to write allotted a lot of this code right it works on that dry principle do not repeat yourself and a lot of these things just come built-in out-of-the-box and it it speeds up your development time saves you a lot of those new code lines because every new code line that you write there's an additional chance for an error so the amount of code lines you can do use less errors you will make plus your code just looks beautiful and so much more readable right get object or 404 instead of try that it added up accept object does not exist right so it's confusing okay let's go on get object or 404 this is some advanced stuff we're gonna skip that for now now that says use the template system so in our detailed HTML we're gonna go and we're gonna paste that there save it and let's see what it's saying back to the detail view for our pol application here's what it should look like okay so now let's go to the detail and let's see first let's just see what the result is right and then we'll talk about it I'll hit enter what's cracking is showing it to me in a nicer way let's do two it's showing it to me in a nicer way what happens if I do four same thing no question matches the given query okay so what are they doing here did take your a particular question and they wrap it up in h1 tag in HTML anything that's an h1 tag will make it's called a heading one and you can go all the way down to heading six heading six being the smallest heading one being the biggest boldest so we have an in a heading one and then right underneath it we say four choice in question dot choice underscore set at all so for all of the choices as like at choices as in the particular answers you can have two each question right though those answers are the ones that actually get an upload or there's no down of those so an upload show me all of those answers except in this case we don't have any answers or any choices so that's why it's not showing those how is it working the template system uses dot lookup syntax so you'll do question doc question text so go into the question and then it'll access the question underscore text another thing to notice if you're more advanced if the question of the score text attribute did not exist it would go and try to access it as a index okay so imagine if it was like if you did question dot zero or something like that so you you're still using dot notation so that's something that could trip you up later okay it would have tried a list index lookup okay and then method calling happens in the for loop so this for loop method calling is happening there because you know you're doing question that choice underscore set dot all okay and then it's interpret as Python code which is cool like right here and it returns an iterable of choice objects and then we iterate over it right so that's why we use a for loop and then we iterate over it now remember when we wrote the link to a question the pole slash index dot HTML so let's go to our pole slash index dot HTML you can see what we did here right so we said slash so for the links we said slash polls slash this right so let's go take a look at this page one more time we're gonna go right here and I'm just gonna hit holes and hit enter so you can see how it's showing me what's cracking what's new what's up so each of this and when you click into it you go into its detailed view right kind of like when you click into a blog post and also under what's up you can actually see the choices so that's just a recent thing we just added right so how is that working how are we linking it and how we're linking and we're saying slash polls slash that question ID which matches one of the paths in our URLs that py so if I take you to my URL it'll match the path of this guy and that takes you to the detail view and that's how we can see the detailed view so let's go back but we're hardwiring the URL paths this way if we have a lot of URL paths our logic can get messed up or if we change if we go in our URLs up he why and I I don't know change this path to be something else right it has like it says polls here again followed by something else it could like mess us up so what we want to do is we want to use it in a dynamic way so that's what they're showing us here so they're saying hey replace this guy with this guy so we're gonna do just that okay so we're gonna go back to our index.html and we will just do this okay so now what this is saying is for the URL use detail detail as in this that's where we're getting the name from and then as the argument pass in the question ID so this question since we're looping over it if you do question dot user dot notation it'll get you that particular questions ID so question that ID that's what you pass in as the argument and so then when you go to your URLs to this question Aidid that's what it's gonna pass okay that's essentially it and I believe you should also be able to do question underscore ID equals that but in this case we're just gonna keep it like this okay so no need for keyword arguments okay and then in in between here so that's what its gonna link to it's gonna link to the detailed view of that particular question and the text that we're generating is just from right here so that's question and dot question underscore text if you are confused by this look up stuff on HTML okay and look up how links work in HTML again I'm not gonna go into too much HTML because that's outside the scope of this tutorial okay okay and that's how it's essentially working now namespacing URL names it's basically just telling us like hey look so let's go down over here let's just make sure we're not missing anything okay from the top okay so they're saying if you want to change the URL to something else perhaps is something like pole slash specific slash 12 so this is something that I've already said but but they're just reiterating here and they're saying it like if you change the URL it's still gonna work now that we change it to be the dynamic way okay so now we can change our URL pattern to whatever and it's not hardwired anymore and now they're talking about name spacing URL names okay so this way you want to make sure that all of your apps actually have there's no way for like right now we only have two poles app right but what if we wanted to have more apps inside of this then what could happen is that if any of our HTML files name matched so let's say you have a poles app and it has an index.html file right what if you had a blog app and that also had an index.html file now you're gonna have a collision so what we do for that that's us that's the reason why we create under templates another folder called poles and if we want it to add templates for a blogging app that's why we would have an app called blog and then under that we'd have templates and then under that we'd have blog and then under that would put our HTML stuff okay that's the reason why we do it like that and this way there's gonna be no collisions okay now change your poll slash index dot HTML so let's see what it's saying here so we can have our app name app underscore named Abner our name is equal to polls so this is in our URLs a py so let's go right there URLs not py and they're saying hey add that guy right over here okay and pretty much everything else we want to just leave as is now change your poll slash index dot HTML template from this to this so in our index dot HTML we're gonna go right here and we're gonna paste that guy so notice the difference all we did is we do polls equals D P polls colon detail okay that's really the only difference there's no other difference I'm gonna save that and this way our URLs will never collide and everything has a proper name spacing right because right now we created a detail for our polls what if you want to create a in what if you had to add a new app called blog and then create details in there you don't want it to just be called detail because it's gonna collide with the polls detail but now because it's polls : detailed then later you can do blog : detail or Instagram post : detail or Facebook status : detail and this way you can differentiate all of those different apps you have within one of your Django projects ok so hopefully that makes sense hopefully this tutorial was helpful for you that's it for this video I do got to let you know that we do have our flagship course called the profitable programmer coming up that's to help you become a web developer become a badass in Python and Django and everything to do with becoming a web developer as you know as a Python web developer and we also show you how to get clients and earn an income and much more in depth ok there we're also gonna go deep into Django and help you create apps and host so if you are interested in this please go to this link over here and join our VIP waitlist and as my bonus to you I'm gonna give to you your Python earning potential calculator and it's really cool I'm gonna give you that for free okay so go to that link right now and join the VIP waitlist but that's that that's it and I'm gonna see you in the next part of this tutorial\n"