2. Django Structure – Django by Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello world Noah here welcome to the next episode of Django by example in this episode we're going to talk about the structure of the Django web framework essentially how does Django take a user's request for specific page and turn it into a response some HTML that contains all of the information that that page needs this is a diagram and we're just gonna walk through each step of the diagram and explain it in general not gonna get super specific yet but in the next video we're going to create a new project and look at the project structure and see how each of the files that are part of the project relates back to this diagram so the first step in the diagram is the web browser that's the end user that's trying to access a specific page on your website you'll notice it's in grey because it's not part of Django it's just one of those browsers probably on the screen maybe it's somewhere on mobile even on a television wherever it might be but it's some web browser that's trying to get a specific page and so the part with the green background is Django that's going to handle the request from the web browser and in the end give it back to the web browser so the first step on our journey is the URL dispatcher and the job of the URL dispatcher is to take the URL that the web browser is trying to access and map it to the appropriate view so for example the first example is slash which would be the home page or the index page so for example just going to example.com so in that case we want to go to the view that's in charge of loading the home page if they go to slash about we want to go to the page of worth the about information maybe information about the website if they go to the page slash user slash and then some ID number we want to show them information about a user and whichever user they want to see is determined by the ID number so for example slash user slash one would show information about the user with ID 1 so in all cases it's going to take that and put it to the user page view but the data that the view generates will there based on which user were trying to take a look at we'll get back to that in just a second before we take a look at views we're gonna talk about templates and models first because the view is essentially the bridge between models and templates and I think it'll make more sense to do it this way so a template is some HTML code that basically has some blanks in it some you know data that needs to be filled in by the view so if you take a look for example at this HTML it looks fairly straightforward but if you look at the title for example it says about and then it says user dot name so whichever user we're currently talking about we want to find their name we don't know which user it could be it could be any user in our database there could be millions of users in our database but whichever user it is we want to find their name same exact thing for the h1 and then in the paragraph text we want to figure out how many friends this user has so we can say user dot friends count and the other part user dot friends that count pipe pluralize will basically just give us an S if the count is not one or nothing if the count is one so it'll say either friend or friends based on the count don't worry about that we're gonna get to that later on but we'll see an example of how that works in just a moment so again the template is basically just the HTML with a bunch of blanks that need to be filled in now models are definitions of data that we need to store in order to make our website work so in that template this was to show a page about a specific user so we want to have a model that stores information about each user so in the case of our application very simple the user just needs to have name which is a char field basically just some text and friends which is a many-to-many field of type person essentially just a list of other people that that user is friends with I guess that you really say my name's money field user instead so it's just a list of users all of this data is stored in the database and you'll notice that the database is gray this is not part of Django you can use pretty much any database that you want you can use SQLite - ql you can do use a text file or XML there's lots of different possibilities for our example we're gonna use SQLite because it saves the entire database in a single file on the disk instead of being you know a process that runs in the background so essentially the model is just a definition of data that needs to be stored in the database and now we can get to the view the view takes specific instances of models and puts them into templates so for example let's say that we have a user with name Noah and friends is a list of other people or other users with the names Eli and Max so in this case we would go to say about and we want to know the name of the user well this particular users name is Noah so it would say Noah same for the heading and for the paragraph it would say well there's two people in that list so it would say the user has two friends the plural eyes would put an S there and indeed we can see that when we insert that information this is essentially what the page looks like it puts in Noah for a username it puts in two for the number of friends that I have and when I go to use PluralEyes it will put an S because two should be plural two friends not two friend and so finally all of this data can go through the caching framework if you want to store data although we're not going to use that and it will go to the web browser so you can see what it looks like at the end the title says about Noah and then you have your heading that says Noah and then it says this user has two friends so that's essentially the the the flow you would in your web browser request for example maybe example.com slash user slash one it would go through the URL dispatch will say okay you're trying to look up a specific user it would grab the data for that specific user from the database and put it into the model the view would then take the information from that user and put it into the template aka the HTML and then it would return the data from the template which in this case is HTML with all of your data filled in' and the web browser can successfully render it now this may seem like a ton of stuff happening but it's instantaneous so just because there's a lot of steps doesn't mean that your website will be slow to load this all happens incredibly quickly now in the next episode we're going to create a project and take a look at the skeleton essentially all of the files that Django generates for us and we'll notice how everything sort of works based on this diagram as always subscribe if you want to see more common with what you want to learn like this video click the like button and continue on to the next episode to learn more about Django
Info
Channel: Noah Rubin
Views: 25,662
Rating: 4.9574466 out of 5
Keywords: django, web, framework, python, python2, python3, flask, backend, frontend, html, css, javascript, model, view, controller, mvc
Id: u49eOt8XJA4
Channel Id: undefined
Length: 7min 12sec (432 seconds)
Published: Sun Mar 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.