Django Web Development with Python Introduction

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on everybody and welcome to an update to the web development in Python with Django tutorial series it's been about three years since I covered Django so I think it's about time to do a little bit of an update plus everybody had been asking for something a little more in-depth last time so hopefully gonna kill two birds one stone so here we have the Django homepage you actually don't have to go here but this is their website they've got documentation and all that definitely check that out they also have a pretty decent tutorial that's worthy of following as well especially if you guys are getting confused on you know how things work in Django and stuff like that so Django is kind of a high initial investment for you or at least for most people because the understanding had this whole model-view-controller thing works and all the connections that need to be made can be kind of a challenge initially but I promise if you stick through it's definitely worth it in the end because as your project grows and you need to make changes or add new things using something high-quality like Django is gonna make it unbelievably easy for you down the line it's just a little harder to get started but hopefully I can make it easier for you guys so let's go ahead and get into it enough chitchat I think the best way to learn Django and really anything is to just do it I'm gonna assume you guys know the basics of Python if you don't follow any basics tutorial you can go to Python program that come to the fundamentals boom there's a basic series there or anyone else's just just know the basics of 5 ok so let's go out and get started so the first thing Oh couple other things I'm going to mention is I'm gonna be using the sublime text editor you don't have to use sublime text you could use any editor you want you could use notepad plus plus or notepad for that matter or pycharm or whatever use whatever you want the other thing is I'm gonna be doing some of the development on a Windows machine and then later we'll be on Linux server since eventually if you're gonna deploy a website chances are you're gonna be on Linux somewhere so anyway your operating system doesn't matter I've also done a lot of development on Mac I've done it on Windows and I've done on Linux it just doesn't matter so we're going to be using Python 3 point-seven here so also I'm assuming you've got Python installed already and with that let's go ahead and get started so the first thing I'm gonna do I'm gonna hopefully not drool out of my mouth and open up a command window and we're gonna go ahead and do a pip install Jenko if you're on Linux or Mac you might say instead like Python 3m pip install Django or something like that if you're on Windows it's PIP if you have multiple versions of Python then it would be like PI - three point seven MN Stahl okay I'm assuming like I said though you guys already know that so take note we're using Django two point one point five here you don't have to use the exact version I use just know that as things progress things might be a little different from what I've seen a lot of things are still backwards compatible and it's like totally fine things aren't getting deprecated but sometimes things are so if something's not working out for you for whatever reason you could try to install the exact same version as I'm using to see if it's just a difference in version so for example if you wanted to you could also say pip install Django double equals two point one point five and that will install exactly version two point one point five now we'll just say before you deploy your your creation make sure you're using the latest version of Django there's lots of things that change over time one of those things is like security fixes and stuff so if you're using an outdated Django you you might be making a mistake so so the only reason you should install a specific version that I'm using here is just so you could follow along and just learn about Django and then later learn what what's the difference between let's say Django 2 and Django 2.5 or something like that but probably unless this first number changes like Django 3 something you should probably will get away with following along anyway let's see if we're done with thee we are done with the install when you install Django you get a little command line thing called Django admin we can do all these things with Django admin but the main thing that we're gonna do is start a project and trying to think here that's probably one of the main things that you're actually going to use Django admin for apparently you can do quite a few other things with Django admin but instead you'll be using a different script so anyways what we're gonna say is Django admin start project and we're gonna call this project my site the way Django works is it assumes all web sites are actually just a collection of apps it could just be one app for sure but it's probably a collection of apps so consider a website that has like a forum store and like a blog or something like that okay each of those things are separate apps you have a forum app you've got a Store app and a blog app okay that's how Django sees things so when we start a project our project is called my site and within my site we're gonna have a bunch of apps that do things the thing that we're gonna do is we're gonna create a website that's like Python programming dotnet I've been considering switching Python programming at net over to Django currently it's written in flask and for the reasons I mentioned before as the whoops as a project grows in flask trying to add new elements and change things over time becomes very tedious with Django it's just not so I mean this always makes me think of a comet comic I've done it again Wow if I could type haven't I I bet if I go to images hopefully we're not seen yet hopefully nothing on here is what we should save searches on anyways this is the comic I always think of you know you start off a project you think you're doing great and then at the end of the day it looks something more like this and initially as we're setting up Django it's gonna feel like we're doing this but the thing is that's as complex as it will ever get as opposed to a project like Python programming at net where there's so many little connections going all over the place it really is a challenge I I do think it's worth it to use Django long term it's just hard to initially get into it but anyways start project my site boom let's create my site and what's gonna happen is we should have a new directory we sure do called my site this is your your project right inside of your project you have another directory called my site and then we have managed PI which is actually what we're going to use to do much of the things coming up so inside of this my site directory this is kind of like an app already but I'm going to refer to this there's probably a special term in Django I just don't know it I'm gonna call this your like primary app inside of this app really all this app is gonna do is point to your other apps you're not gonna use this you're gonna actually develop in here other than to point to other apps and adjust the settings other than that you're not gonna really do anything I'm just going to zoom in a little bit so coming back out to this directory we want to play with manage pie so I'm going to open up a command window here if you hold shift and right click by the way that's how I get that you also if you're on Windows can just do CMD and that should also open up a command window right where you are so the first thing that we're going to do is start an app so I'm gonna say let's say pie - 3.7 I actually can just say Python because I don't have one version but anyways pie 3.7 manage pie start app and then we're gonna call this app main so this will be like the main core of the website so for me this is where like we're gonna put like tutorials like the main aspect is tutorials and if anything I could probably call this tutorials as well but I'm probably gonna want to put other things inside of main besides just tutorials so I'm gonna call it main for now so we've created an app called main if we come back we should see we've got another directory here called main if we open that up it looks a lot like our my site directory or our primary app but we've got actually some extra things here and as we can see we've got models views where's the controller we actually don't have any controller here but we'll get there I promise so what we want to do now is let's go ahead I'm gonna get this just to stay running now so what I'm gonna say I'm gonna move have to command prompts here the first one I'm gonna do is we're gonna use manage PI and we're actually just gonna say run server run server and what this does is runs your development server you in theory could run a whole website off of this it just isn't recommended probably for all sorts of reasons including security reasons anyway you can see we've got some migration issues that we can worry about later but mainly we can see that the website is now running at basically local host on port 8000 so let's head there let's see what we got paste okay so you get this little beautiful imagery and it just kind of notifies you hey you things are working sort of up to this point but rather than having this kind of display I think it's a little cooler to have you know control like how do I change all of these things so let's get into it so first of all with the Model View controller it kind of works almost like in Reverse of that you've got your controller which basically takes the URL that whoever typed in or clicked on a link that went to that URL the controller takes that URL and maps that to a view and then that view returns the template now most of the time that view is also going to interface with your models but it doesn't actually have to really you just need a controller and a view to return something so let's go ahead and do that just so we can start to pick up how all of this this stuff works so the first thing that we're going to do is let's just create inside of main here we don't have a URL spy so we need to create one the other thing we could do is go into my site take this URL stop hi I'm just going to say copy that and then I'm gonna come into Maine paste and now we have a URL spy I'd love to know like why it doesn't by default come like when you start a new app why doesn't have a URL spy by default if anybody actually knows that'd be cool so this is like the first time I'm using this this machine so I've got to add sublime text really quick to the thing that I want to open great the other thing I probably need to do before I forget indent using spaces done okay so this is what our URLs by just make sure you're in Maine not my site actually I think it would make most sense to start in in the my site you were all stop hi because when someone first comes here what its gonna do is its first gonna look to your primary app and it's gonna look at that URLs dot pi and then it's going to see do we have a URL pattern that matches and it needs to have a URL pattern that matches but generally your primary app is just going to have a pattern that matches and points to some other app so the first thing we're gonna actually do let me go file open folder and then let's open my site and then I'm just gonna close this and I think this is how we're gonna interact from here out so I'm gonna go into first let's go to my site URLs pi so primary app I switched around don't get confused it's your fault so what we want to do here is we just need to take this because it's our primary app and point it to our new app called main so we're gonna add another path here I'm just going to copy this come down here and I'm gonna paste and then what I'm going to say is we're gonna well first let's also import include and then what we're gonna say for this path is an empty path so homepage no other URLs are being submitted here we want to include main URLs so whenever someone comes to just just the home page and there's nothing else that this path is gonna match so then it's gonna say okay let's go to main dot URLs now so then we're gonna go to main URLs make sure you're in main URLs and now what we want to do is actually we don't need add in here so I'm gonna delete that for now and then we are gonna say and we don't need this admin either will keep path and then the other thing we're gonna do is from just relative import and then we're gonna pass an app name and we're gonna say app name equals main this is useful later on when you're trying to create custom URLs that will point to a specific view but you don't actually have to hard code the URL it's all totally dynamic so right now we're not using that but I just don't want to forget about it later and you might as well just always throw this in because eventually you'll want to have that functionality and you'll try to use it because you saw it in an example somewhere or something and it's not gonna work and you're gonna be like wait what so anyway we'll throw that in there for now so then we're gonna say a path and again the path would be homepage because like right now all paths will lead to this URL spy so but for now we're gonna say okay the empty path is gonna point towards a views dot home page and then we're going to give this also a name called home page I'm gonna put a comma here before I forget because later on we'll add more paths and throw a syntax error if we mess that up okay so going back over someone comes here they visit the home page first we go to our primary app comes here says oh hey look here's a path that matches what they just put in there's nothing else here cool what do we do include main dot URLs fine boom points to main dot URLs we come here path ah okay this points to a view and it's the home page function in that view okay great unfortunately we don't have the view done so if we come into our main area here and in fact I'd said I was going to keep using this so I think we'll keep using this if we come into main we can click on View spy and here we have what we need to bring in views now render is used to render templates I just want to pass a simple string right now I don't want to get into templates so the next thing I'm gonna say is from Django HTTP you or HTTP response now define homepage and then we're gonna pass a request so basically all your actual views if you have like a helper function or something and obviously doesn't need requests but for any actual view you pass requests always and then obviously this is the name that we passed right here right we said views dot homepage so that's why it's called homepage now all we're gonna say is return HTTP response and then we can pass actual HTML here even so we could say wow this is an tutorial and you can feel free to put whatever you want in there I'm gonna say awesome okay save that great so now let's make sure our servers still running did we hit any errors I don't see anything just yet so let's come over here let us refresh the page boom let me zoom in a little bit just so everybody can see how what kind of a tutorial oh it's awesome tutorial okay I think this is a good stopping point before we get into the next tutorial which is where we're gonna talk about models and stuff like that and quickly start to ramp up but the big thing is you have to understand even's is this basic stuff cuz that was actually a lot of work to go through just to return some text on a page so what I strongly suggest you do is after this video before you go to the next one is try to do everything we just did on your own without needing to look anything up I mean obviously you might forget some of like the import names or something like that but even then it's just denko HTTP HTTP response and then the only other import you need is the include here so you should be able to do this completely on your own now and so what I would suggest you do is create a new you know start a new project and try to do all the things that we've done up to this point without needing to reference anything obviously as you as time goes on that won't be necessary that you can like make an entire website without being it'll look anything up but it's um or not that you understand how these initial connections are made before you continue on because it only gets a little more complex from here so anyways that's it for now a quick shout out to my most recent channel members Dinesh Kumar Luciano Cena who knits and Nathon II and you day manchanda thank you guys very much for your support you guys are awesome also side note this series is sponsored in part by Linode which is who i've actually used to host Python programming at net for like years also they're my basically they're my main host for like my larger websites and we'll be using Linode as as our deployment Virtual Private servers down the line but for quite a while we'll just develop locally because I think that's really the way to go anyways but if you want to check out Leonard or you need a hosting solution I'll put a link in the description from them but later on will actually go through how you can actually launch a django application on Linode and actually you know why something like Linode is better than some of the other options that you have out there trust me I've used all of them so anyways that's it for now questions comments concerns whatever feel free to leave them below also we have a discord service just discord GG slash centex in your browser get you there come hang out come chat with us ask questions whatever tell me how how I can improve my tutorials all that kind of stuff anyways I will see you guys in the next video
Info
Channel: sentdex
Views: 221,298
Rating: undefined out of 5
Keywords: django, web development, website, tutorial, python
Id: yD0_1DPmfKM
Channel Id: undefined
Length: 19min 13sec (1153 seconds)
Published: Mon Jan 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.