Why Django - Day 1 - Django Bootcamp

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to day one of the django boot camp thanks for coming out my name is justin i'm going to be taking you through this now this is a little bit different than a lot of my tutorials in the sense that one it's live number two is i'm actually going to be doing a little bit of conceptual things before we actually start coding so conceptual first coding and then live q a after that uh so that's generally the agenda and we will talk about that in again in just a moment uh but just a little recap as to what we did in the last one so in day zero we introduced some of the things that we're going to be talking about i'm probably not going to be going back too much unless it pertains to what we need to do for example i'm not really going to be covering all of the different configuration features inside of settings.pi if you don't know what i'm talking about no worries we'll get back to it but i will be covering uh things that pertain to settings when it actually comes up inside of django that's one little uh housekeeping thing that i just wanted to mention before we jump in okay so yeah of course day one of django boot camp of probably 10 days if we need to extend it for whatever reason we absolutely will it's the first time i'm doing this so you know day one is pretty much the kickoff for all this day zero we had a little bit of hiccups with some audio so what you see of day zero right now is well pretty much just the second half of this whole thing and again here's the agenda we're gonna do a quick little conceptual overview of things we will do some live coding so we will be building an actual project and the project we're building is a single page e-commerce application what that means is we'll have a single product that will be able to sell so it's cut sort of like a one-click sell you'll see this a lot of times when people are selling one item whether it's a course or a book the reason we're not doing a shopping cart and all those things is it adds a lot of complexity and it's really hard to cover all of that in 10 days where doing one single page e-commerce we can absolutely cover in 10 days but potentially a few more we'll see so i'm going to assume you're a little bit like this guy and that is you don't know nothing about django or very little about django right um and even if you do it's gonna be a little bit of a overview of things for you so let's talk about the actual requirements technically speaking that you'll need to be successful from here on out now i totally get it if you don't know these things you're gonna want to stop now and go back i'll give you a resource in a moment of what you will want to cover but if you don't know these things in python like how to create virtual environments this is key because it allows us to package our application in one place without it really interacting with other projects right so we want to isolate our projects as much as possible and of course the concepts in python of classes functions basic math syntax i mean if you know how to do classes and functions you probably can handle django no problem that i mean that's so much of what django is is built into all of these classes and functions throughout that you'll use to render a bunch of cool stuff so of course a bonus would be knowing html and css and javascript this is a bonus because we are building web applications that's what django's all about which is what we'll talk about in a moment but html and css are fundamental to building websites they're fundamental to the web javascript actually just adds some additional features to django but you really don't need to build an application using javascript it's just kind of a bonus knowledge um but then also it'll help you with the html and css side most likely depending on how well you know javascript of course so um if you don't know the requirements that i just mentioned then 30 days of python is where you're going to want to go and ideally up to the first 15 days or so um maybe even up to the first 10 days of that so it's really not that much python you need to know to be successful with django uh in fact for me personally i actually didn't know that much python when i got into django but once i learned django i actually helped reinforce my python stuff and vice versa now what is django written in it's python so django is a python web framework so everything in django is done in python so that also means that the vast majority of things that you can do in python you can also do in django that's not always true but for for all beginners that's pretty much true if you want to learn 30 days of python here's where you're going to go so cfe dot sh is my website i've got all of it there i also have 30 days of python on cfe dot sh youtube this of course is a newer version of 30 days of python it's for python 3.8 and i think it'll be really good for you to do it even if you did the older version of 30 days of python okay so that's some housekeeping things some requirements let's actually get into it now what do these four websites have in common right so hopefully what these pages are showing you that they all have users right so they have users that authenticate with their web application that's one thing and where do they actually store these users well they are all stored in a database right and databases look not a whole lot different to like an excel spreadsheet or a number spreadsheet they're essentially spreadsheets that are just a little bit more complicated but overall they work very similarly and on top of that they have their html and css it's basically just a document that has some formatting that renders it out in a web browser that's it that's what a web app is so notice that i didn't say anything about django i didn't say anything about the specific database when it comes down to it web apps just render out specific lines in a database right so as we see here we see that each post is corresponding essentially to a line in the database so whatever that is you have control over these things quite a bit right and django makes this process a lot easier but it's not the only one right there's many other ways to actually accomplish the same thing and in some cases they accomplish them better than django in other cases they don't right it really just depends notice that i have wordpress joomla and drupal on there those are not web frameworks but they do accomplish this same thing they will always render out things very similar to this now it also depends on how you actually design the application and all that and again this is mainly for conceptual right just to remember that a web application framework or any web application for that matter is just really something that sits between the web page that somebody views like a user actually views and the database and that in between is what's going to parse out what's in the database it's going to update the database and it's going to hopefully securely do all those things now in our case with django we want our users to be updating specifically their user information but it's not really that complicated when it really gets down to it there's a lot of things that are complicated that go into making all of that happen so if we actually look at this example here this is essentially how websites work right you go to a website or a url itself that actually pings a server or really a computer with your web application on it on there that server also may have a database on it where django or ruby on rails or flask or whatever it is will actually look up the data it needs to look up and then it's going to send back to the user some rendered out html page maybe with css maybe with javascript but at the very least it's going to be html of some kind because that's what the browser your web browser actually looks for right so there's other technologies out there that don't use html that don't use your web browser right like that's probably something you're really well aware of like if you use slack you're not opening up chrome or safari or anything like that you're just opening up slack and how that communicates with the server is a bit different than how your web browser does i think intuitively most of us understand this but this is a good example of that so instead of it rendering out or the server sending back html it's going to send back different kind of content that something like something like slack would actually render or any mobile application or any application for that matter so hopefully that makes sense right hopefully you're getting this i think it does but i've been doing this a long time i really really understand this equation here and i want you to really understand also that when you go to a web page it's a server that's going to help parse out what actually comes back from that web page and again i didn't have anything related to django in here this is a very common equation if you will for web applications in general django just in my opinion makes it much easier and that's how django's a web framework is it does this entire thing here right it helps with this entire application or this equation if you will so then the question is of course why do we pick django that is the underlying foundation for using this day one right so in day one we want to talk about why is it that we're using django over all of these other options and i know that i missed a ton of these options but there are plenty of them out there but django the reason django stands among most of them well number one it's a framework not a cms this gives us a lot of flexibility over how the structure of the application works so cms is wordpress and drupal and joomla um they might have additional features to their cms but the idea of the cms is like it's really sort of locking you into here now if you're not familiar with that it's called content management system so what wordpress is made for is for blogging it's for making blogs it's a great tool for what it does but what you don't necessarily do with wordpress is turn it into an e-commerce site now you can because technically speaking you know it's just this right it's a database with some stuff in the middle and some html being rendered out that stuff in the middle can always be modified that includes open source technologies like wordpress but django itself as well as many other frameworks are built to be essentially here's a toolkit builds what you want where wordpress is like here's already built tools that were built with a different tool kit and now you can actually just install it and run it right so it is subtly different but it's drastically gives you a lot more control it's almost like handing you a bunch of tools versus handing you a almost finished product all you got to do is paint it or something like that next thing is it's written in python now to me python is the easiest language to learn as a beginner which also means that using python web frameworks like django like flask like fast api are a lot easier to learn because they're written in python they actually have a really solid basis in python and that's why at the very beginning of this i mentioned these are the requirements right you just need to know generally speaking how to do all of these things and you can be successful in django which allows it to really shine because it's written in python and then the final thing this is the key thing that django has that stands among pretty much other every other web framework now i don't know every web framework and i don't know in in and out like i do django but this idea of batteries included so a good example of this is if you need users on your application if you are trying to build the next one of these you're going to need users on there so django actually has those things built into it right so django has user authentication built right in so it's incredibly easy to start using users in your application now something like flask can do this but it's not built into the core of flask it's a third party package that you need to bring in or a third party software that you need to bring in to floss to make it happen and that's true about node.js and express.js you have to bring in something else there's something called adonis in javascript that might be a really good competitor to django in javascript but really in my research there's nothing out there that is run by python or javascript that comes close to django with this batteries included concept now there is a downside to this and that is you have to learn a lot about how to use these batteries they're not actually batteries right it's just a bunch of things a bunch of features to the web framework that make it incredibly easy to just get started right almost like plugging in a battery and your toy works right that's essentially the idea here um but the downside to it is it makes it a lot more complicated to learn again i don't actually think it's that complicated but there's a lot of pieces going so once you understand all those pieces the complicated stuff becomes a lot less complicated so that brings me to what we should do now let's actually start coding let's actually start building a real project talking about all of these things and more specifically really trying to answer this question of why django so i'm going to be going from scratch so let's go ahead and jump into vs code here and i assume that you've already installed python 3.8 i'm assuming that you have that if you don't have that this is going to be something you'll want to review later okay so i'm going to go ahead and open up the terminal now vs code itself is just a code editor and it allows me to type out all of my code open up the terminal a easy command for it is ctrl tilde if you're not familiar with tilde it is that so command tilde will open up or control tilde rather we'll open up the terminal and we can actually write all of our commands now before i get started what i'm going to do is open up the explorer here and i'm going to find a folder where i want to put my project now in day zero i actually created this dj dash boot camp folder i'm gonna do that again but this time i'm just gonna call it boot camp it's essentially the same thing so no worries if you're working off of what we did in day zero i'm gonna be doing it all over again because that's where we're at we're at day one okay so let's go ahead and open this up and once i do that my environment is much more prepared at least in vs code to do this now the reason you add a folder into vs code anyway is so it's a lot easier to write out all of your code so another thing that makes that easy is actually just putting it into a workspace and again we'll call this bootcamp so these names are arbitrary and what you'll find a lot in code is the names themselves are often arbitrary right so software that's the coolest thing about it is you can rename things however you want but just because it works doesn't mean it's going to work well for somebody else so trying to best build your application like other people do is going to help you a lot in the long run because then you'll clean things up a lot so stick with me on the naming conventions is the point there okay so let's go ahead and start our general project again i'm going to open up that terminal with control tilde and inside of here listing everything out or pwd if you're on windows you're going to be using dir for those same two commands um so i actually am going to create a virtual environment in here and again my python 3 is 3.8.2 the key thing here is 3.8 the 0.2 doesn't matter nearly as much especially while you're learning so you can absolutely use an older version of python uh but i like using the latest version that's incredibly stable so python 3.8 is where we're going here so python 3 and then dash m we'll go ahead and do v e and v so what this command is the dash m means python module and we're looking for the virtual environment module now if you've used pip bmv or other virtual environment managers and you know those well by all means go ahead and do it i do not recommend using anaconda or conda for this django and anaconda just don't play that well together so using the built-in virtual environment manager in python 3 is what we'll be doing and i'm going to go ahead and put a period here all that signifies is it's going to create the virtual environment inside of this folder if i actually used a variable or some other name instead of this period it would actually just create that folder and that will be the virtual environment but i like putting it in the root of my project so let's go ahead and put it there and there we go so now i'm going to go ahead and activate this virtual environment once it actually actually finishes so we'll go ahead and do source then activate that's for the mac and linux users for windows users it's going to be just like this so dot slash scripts slash activate or lowercase activate that will actually execute it on you windows users and just so you know what i have here might change right there's going to be things that i'll have to do that's not live that will change just slightly but the vast majority of the code will not change okay so naturally i need to pip freeze i want to just make sure i have nothing installed here this means i have a clean virtual environment um and when in doubt if you don't have a clean virtual environment just delete all of these old folders even the entire bootcamp folder and create it again it's really not that hard okay so let's go ahead and do django dash admin and start project and i'm gonna call this bootcamp and again i'll put a period here i don't need a sub uh folder in there or subdirectory that has that and i get this command not found so this is not an error i mean it is an error in the sense that the site doesn't know about it or the the virtual environment doesn't know about it but if you don't get this error that means that you did something wrong with your virtual environment most likely or it means you already installed django now the version of django i'm going to be using is django 3.1.2 this doesn't actually matter that much honestly the version of django does not matter that much for the probably the first five maybe even the whole thing and the reason i'm saying that is because django itself has evolved over the last 10 years for sure without a doubt it's evolved but it's not so much different that django 1.11 is going to absolutely be way different than django 3.1 um so that's actually something really important to note because you will find a bunch of tutorials granted a lot of them are mine but you'll find a bunch of other ones that have a different version of django that you can absolutely still work with you can still learn from because the core concepts of django will probably never change this is something that the django developers have been adamantly talking about with all sorts of things as well as all the new features that have yet to come out so pip install django and then equals equals to 3.1.2 so those two equals are important that actually gives me the version and again that version is written right here when in doubt if you're not sure what the version is you can click on that download button or just go to djangoproject.com download and find the latest release now i always recommend that you don't always just jump to the latest release but rather use well number one use the version that's in the video that you're learning from whatever that version is but more specifically when you go into production when you actually bring this to the world and not working on it on your local computer and learning you want to use the lts versions lts versions stand for long-term support and as you notice django 2.2 is going to be supported longer through 3.0 and 3.1 it's going to be supported well into 2022 and it will absolutely work into well 2025 and beyond right so this code even if it's older it still works it still runs the support that they give is usually related to security bugs every once in a while but they usually only release as almost as bug free as possible code especially when it comes to an lts and that's what those last numbers are for so if you see on the the right-hand side here these are no longer receiving security updates or bug fixes look at that django 1.0.4 so you can even install jenga 1.0.4 and attempt to create a project there so i have to hammer this in all the time because so many people want to debate me on this so many people that don't know django super well always say oh you shouldn't use django 1.11 while you're learning i i couldn't agree or disagree more with that statement uh because january okay rant over let's get back to coding i'm gonna install django so once you install django you get commands like the django dash admin command um to actually get what those things are you just type out django dash admin and it'll tell you all of the various options for that so django django-admin and we hit enter this gives me all of the options that django itself has now on your command line right so of course i'm working in the command line here but at any time if you are in doubt you can just run django dash admin and you can see all of the things that are related to django and this is also a good list of things that you might want to learn about right so db shell what does that mean check it out dump data what does that mean and when i say check it out i mean you can either google it one that's an easy way uh the other way which is what i learned from directly this is how i get better at django is by going into the django documentation and just reading the manual you know what i mean okay let's get back to it so now let's go ahead and create our project so django-admin start project and whatever we want to name the project in this case i'm going to name it boot camp and again i'm going to put that period there i already mentioned why okay so that creates our django project it's now in this folder called bootcamp now one of the things you want to be aware of is you don't want to call your project ben you don't want to call it include or lib so there are a number of folders or project names you might not want to use and django will warn you about that so if i try to start a project like um this is my bad name it gives you a validation error but it's not going to do this every single time it's just something to be aware of that you should be using dashes instead you should be using snake case which means that you use underscores like that so that's nate case notice that everything is lowercase and then spaces are replaced for underscores very common thing that happens that you're just like why did that happen that's why okay so i'm gonna clear this out and i have the django settings already installed but i'm not even gonna look at that right now instead what i'm gonna do is actually show you some of the really cool features that are built into django by default this is the batteries included thing so django dash admin and we're going to go ahead and do start app and i'm going to call it products start app products we do not put a period at this point you probably won't have to use periods anymore so that's one way to call it another way is to say python manage.py start app and then something else that you might want to start crap like profiles okay so i might may or may not use these apps but that's how you do it now the first one is actually not always preferred because manage.pi is right here so after we created this project manage.pi was also created and this is actually how you manage your local project inside of your virtual environment django dash admin doesn't necessarily take your local project into account python manage.py does that's really the difference another another cool thing about manage.py is python manage.pyshell this is a django managed shell it's to the best of my knowledge other frameworks don't have this by default python does or django does where we can interact with stuff in here we will come back to that in just a moment um okay so now that we've got this done we've we've kind of built a couple applications let's look at the first application and jump into model stop high okay so models.pi is actually how we want to correspond to our actual um our database entry right so what we're trying to do here is something like this okay so this of course is for a blog post but here's our database here i actually am wanting to convert what these things are to being managed by django right so i want django to actually say hey this is a table these are the rows that's what we want django to do so django again is sitting in between these things so we really want to manage what's inside of these tables now this is completely up to you on how you decide this this is another advantage of using a web framework this is true for all web frameworks django just has it in by default this is the batteries included thing so if i said class product models dot model i'm going to go ahead and write past for a moment this actually inherits from the django models itself right so if you don't pass classmodels.model this is just a standard python class this will create python objects that's it it's not going to do anything else but if we do model stop what this can eventually tell django is hey what we want to do is take that model and make a table and align it in some sort of way some sort of fashion that we can use on our views and all these other places that we'll get to so by default django actually has an id field so it's models.auto field django has that by default i'm going to comment this out for a moment so what i'm going to do is say title and do models dot char field or car field and this stands for just character field and we have to add in a max length like how many characters do we want to have in here so i'm going to go ahead and say 220. okay so right now the what this actually will end up looking like is if i go back in here let me just quickly show you this it's not going to have four it will literally have two columns and it'll be id and title that's it right so that's what this model now corresponds to and this models.char field is really just telling the database what kind of field this is it adds some additional validation which we'll talk about validation later but it's just saying hey database this field right here is a character field so treat it as a character field in the database so that means that it can be letters or numbers or uh punctuation it can be all of those things in this field now that's different than something like let's go ahead and say price and do models dot uh we'll we'll just stick with something simple for now and we'll say integer field and i'll give it a default of zero so an integer field then can only be an integer not a decimal not a letter only an integer this is both on django as well as on our database so this will be price and it'll be actually enforced in both places so that starts to get into a complicated territory before we actually jump there let's actually take a look at the different field options in the django documentation so you can go into the january documentation search or you can just do django fields inside of google google is going to be your best friend while you're learning all sorts of things as you may already know and you can go to the model field reference so this reference itself gives you a lot more details about the things that i just mentioned um and then if you look on the right hand side you'll see this field types here you click on that the very first one is that auto field this means it's going to automatically increment the field and if you're familiar with database technologies this is the primary key as well which is also cool um so there's all sorts of fields in here there's a binary field a boolean field so true for false values the char field or the car field again um that's the one we used so when in doubt always go to the documentation to learn more but also when in doubt just use the basics we've got char field and then we can also do let's say content as in like description models.txt field now i don't want the text field to be required so i'll go ahead and say null equals a true and blank equals to true so blank equals to true means that django is not going to make it required null equals to true means that it's can be null in the database um so there is some debate to ask if we should do that or not but um so we've got this model here now now what we need to do is actually bring it into our database because we haven't created a database yet we haven't created users yet we haven't done any of those things but we will in just a moment but before we do that let's flush this idea out even more let's go into profiles into models here and in this case i'll go ahead and say class profile and again it's models.model and this is where we're going to define another model what is it that we want for a user profile you know perhaps we just want to have like some sort of description or we can just say content and do models.txt field and make it required you know perhaps we want to have an image or multiple images perhaps we want to have a location uh perhaps we want to have like their screen name or their like actual name uh either way that part doesn't matter too much this is this is where it's up to you it's kind of a blank canvas as to how you're going to want to go about doing this and creating the data that you want to use web frameworks are really good with managing that data so by all means play around with those as much as possible after you finish a model what you're going to want to do is make sure that inside of settings.hi you scroll down to these installed apps now remember we ran that command of start app to me this is a little bit confusing because the entire thing is a web app it's not like each one of these things is not a new web app and it's definitely not an app like how mobile phones work right you're not installing an app here so i actually like to think of these as components so these are installed components to the greater web app that's how i usually think about it so we need to add those apps into our installed apps here and this is true every single time when you start a new app you just add it in here and this is just so django knows hey not only is this an app that django should know about but it's also an app that the database should probably know about especially if there's things in model stop high for both of them in this case i'm going to leave both of them saved right with the data that they have okay so i'm going to go ahead and do pro products and profiles okay so we save that and now we're going to go ahead and run another command that you'll become best friends with well actually two commands and that is python manage.py make migrations and python manage.py migrate okay so what just happened here well several things make migrations call is essentially saying hey database django wants to make some changes that's it but it did not make those changes yet until we ran python manage.py migrate now what's going to happen especially if you're a beginner is this is going to fail it's going to fail somewhere you can get really frustrated so what you're going to want to do instead is you're going to want to jump into these migration folders or maybe your whole project and literally grab everything except for init.hi grab this and drag it into your trash go to your other app and drag it into your trash and then locate to where your database is in this case django has a built-in one called sqlite3 this is perfectly okay for local development or testing um and i'll talk about databases another time but we're going to go ahead and drag that into the trash as well so now that's dragged into trash what can we do we can fix any errors that we might have had in our code or we can just try this again with make migrations notice it does the exact same thing right no surprises here to me but to you it was like well why did it do that well again it's telling django or excuse me it's telling the database we are now ready to make some changes but don't make them yet we're just ready to do it if i run make migrations again it's going to say no change is detected that's because the actual changes are already denoted in those migration files so every single one of those migration files opening up we can see this is telling the database what needs to happen make a new table for the profile model and here are the default fields for that notice that it added additional uh features to it that you didn't add more specifically that id field we didn't actually write it that's because we're inheriting from the default django model that's a really cool thing and in case you're wondering yes you can absolutely change and customize this but that's quite a bit more advanced than where we're at yet so far um so there we go we now see that that command gets us ready to change how our database actually works it's called migrations because that's we're migrating we're changing the database even if the database has nothing in it you're changing it from nothing to something so if we run migrate again with python manage py migrate hit enter it actually runs or applies all of those changes now you're like hey there's a bunch of stuff in here that i haven't seen before well that is the batteries included that's what i've been talking about over and over again so in installed apps you have pre-built applications or pre-built components that build up your web application and these are them right here that is what we just saw so those pre-built ones are what's being migrated now in some cases a app or the django app the component itself doesn't have models right and if it doesn't have models it's not going to change the database so let's actually see that i'll go ahead and do python manage.py start app and we'll call this emails okay so i haven't done anything to emails i may or may not use that in the future it doesn't really matter but i am going to add it into the installed apps like we do every single time and then i'm going to go ahead and run python manage.py and make migrations and we see no changes detected typically speaking when there are no changes you don't actually have to run python manage.py migrate but i have that as a habit all the time because python dot py migrate will also alert you that something did change right so let's go ahead and say that in here in this profile model if i say null equals true and blank equals to true and i save it and now i just run migrate it gives me uh your models have changed and and they're not reflected yet so you definitely need to run make migrations to uh count for those changes how cool is that built in to django okay so let's let's actually bring it back i'm not going to do those changes i don't need to so i'm going to go ahead and clear this out and now i actually want to see these things in action like i want to see this data in my database so to do this a very simple way to do this without jumping into a database is to import it into the admin so from.models we're going to go ahead and import product and we'll do admin that's site.register and we're going to register this product in the admin don't worry if you don't know what this means yet i'll show you now we're also going to go ahead and do python manage.py create super user so createsuperuser this command is creating a user in the database so you have to have a database already existing you have to run migrations you have to do all of those things that we just did right so you have to run all of this you need to see these things happen before you can run this and it actually work then you're going to create a username in my case i'm just going to leave it as cfe you can add an email address if you want we're doing development right so we're not this is not a sensitive project at all it's just a basic project okay so we're testing things out so you can use whatever password you want here including like abc or you could try abc abc probably won't work but you can try it and a quick sidebar as to why it won't work in our settings module django has another built-in feature a built-in battery if you will of off password validators so really simple crappy passwords will not be accepted that doesn't mean they are great passwords that just means simple crappy ones will not be anyways so what we've done so far is we created the super user notice you couldn't see my typing that's a typical thing i also use the default for cfe and i didn't actually add the email address so now what we're going to do is run the server with python manage.py run server so this right here is our development server it's a really good chance that you've already seen this before and all we're going to be doing is jumping into this url right here and of course if you're on vs code command click opens this up if you're on windows control click will open that up and there you go we finally have a django web location running believe it or not we actually have this is actually a full-on web application at this point um we could have done this a long time ago but we added a bunch of things so let's go into the django admin and let's log in with our user in my case i use during development i use pretty much the same username and the same password for almost all of my projects so i can just save it in chrome so every time i go to this url uh it knows what that username and password is but go ahead and log those things in we log in here and here's your django admin notice that products is in here but profiles is not but also notice that there's users and groups if we click on users um and we take a look we've got our single user here at this point i can add a million users if i wanted to it would it would take a really long time using the django admin but you certainly could do it but of course we don't want to actually make things hard on ourselves in that case so we'll take a look at how we use the shell in just a moment but here's products right so we can actually add products notice the fields that are here there shouldn't be any surprises to the fields that are listed because of models.pi right here right so models.pi in the products app has a model name product that is right here in this admin right so products product and there we go pretty cool huh so now if we wanted to repeat this process we would go into models.pi grab this profile here and then do admin.site.register of that profile and that now will add the profile model in here oops i think i might have had an issue yes so i try to add it into the admin but notice i did not actually import it so i need to import it there's a couple ways on how you can import this you can do a relative import with dot models profile so the relative import meaning that models.pi and admin.pi for this particular one are right next to each other so admin.pi is in the same directory or folder as models.pi so that's how you can do a relative import if you aren't sure how to do relative imports you can do an absolute one by importing from the module itself so that's profiles here and then do.models.profile and now we can register it we save it django recognizes that changes have happened this is definitely a newer feature of django so when in doubt you can just rerun the server with you know control c cancels it out and then pressing up and enter will actually rerun it so we come back in here and now we've got profiles and products in here now cool so some highlights of those batteries in there right so the last thing i want to do is i want to show you how to actually add data from another feature from django that i mentioned before so i'm going to go ahead and control c out of the running server i'm going to clear this out and we're gonna run python manage.py shell so this is the django managed python shell so to use this we have to use an absolute import for any of our models now there's a lot of things you can do in this managed shell but we're doing it where we're using the models themselves we're actually going to put in entries into our database and not do it inside of the django admin because django admin is great but it's mainly for our non-technical staff members like there are other team members that are not technical or if anyone on your development team needs to change something on the fly your developer as a developer you're going to be doing it in code and the python manager.py shell makes that incredibly easy for us so i'm actually going to be using the product model so we'll go ahead and do from products.models import product and then i'm going to go ahead and do product dot objects dot create and now i want to actually pass in the data that i want for this particular product now the data that i want is going to be key value pairs or keyword arguments for what i actually want to store so the keyword arguments are based off of the fields themselves at least the fields that are required so title doesn't have no or blank it's certainly required content well content has null and blank it's not required price actually has a default so it's also not required so these bottom two are the ones that are just not required and that actually gives you all of the options as to how they're not required which is pretty cool so we'll go ahead and say title and our very first title i'm going to go ahead and say a raspberry pi is our first title and i'll hit enter what do you know we actually just created a product in our database right so you might imagine that if you actually put these all in a list you can actually run through a list of these things you could also load in a csv file run through that csv file and also create the the actual products or the entries in your database let's create one more just for good measure and i'm going to call this a touring pie turnpies are really cool i'm going to be talking about them very soon so make sure you subscribe on my channel again everything okay so anyways now we've got product.object.create we've now created two two actual objects two entries in our database we've got one and two now these numbers actually signify to the id of these products so how do we actually find these products how do we actually look them up well we can do product.objects.get and then the id that we want so the first one is id1 notice it gives me that product the second one is id2 gives me that product if i try three well it doesn't exist i actually don't have that in my database so instead of having a database error i get a django a python error that i can actually manage with python itself that also gives me some example as to what's wrong with this item so with that we can also use the same idea to do delete well actually what we would be doing is we would first grab it with the object we'd set it equal to a variable and then you can do object dot content equals to this is some new content for the field content and then obj.save that's how you update it and if you did obj.delete that's actually how you would delete it from the database don't worry we will be covering all those things a lot more so go ahead and exit out of the python shell and let's just verify that this data is in here we'll go ahead and run the server here and go into our admin again and there are our two products okay so all of this is batteries included that is the point here that's been the point pretty much the whole time now there's something that i haven't talked about yet and this is what we'll be looking forward to with the boot camp is views how do i actually turn this store data in our model and in our database more specifically in our database how do i turn that into an actual web page that's rendering something out right so this right here how do i actually take that data now that i can insert it now that i can get it and now that i can delete it how do i take all of those concepts and actually render it out into a webpage of some kind a big part of that is going to be views dot pi so that's actually what we'll be covering tomorrow in day two so make sure you come at the exact same time as it was today for that and with that thank you for watching day one i'm now going to be jumping in to some questions and yeah so here we go let's go ahead and just jump over to the questions here one second hello everyone welcome back to this i need to just pull up my little q a thing for finding all of this information just give me bear with me one moment here [Music] all right so your questions from here on out are the things uh that i'm going to be answering i it's going to be really hard to go back all the way for that and we're only going to spend a little time in these questions for today um we i will spend a lot more time later days but today we are just going to be spending just a little bit of time in the questions mainly because uh it's day one i don't want to spend a whole lot of questions answering questions yet because i think you'll have a lot more questions in the future and looks like i'm having some issues pulling up uh the standard questions that i was using before bear with me once one more second guys thanks so much appreciate it technical difficulties are always a thing i guess huh yep i am uh okay cool we got some questions all right now let me go to more recent ones so go ahead and start asking your questions right now okay so here's uh here's one of the first ones okay so what am i building uh we're gonna be building a single page ecommerce site i mentioned that at the beginning of this one so maybe you missed that part uh so thank you for that um here is a comment based on pycharm so pycharm is a great ide i agree with you i'm not so sure if it's the best one because vs code is free and it's also really really good pycharm is not although i've heard like as it's not free although i've heard a lot of good things about it uh some people really really really like using pycharm uh so thanks for that um so uh let's see here i think i had a couple live chats i wanted to address or excuse me um some super chats oh just a couple things pi plane thanks for coming out thank you for watching and for the super chat appreciate it and um mr kumar or just kumar thank you for the super chat as well appreciate it all right so hey thanks for watching appreciate it any wagtail tutorials that's not exactly boot camp related but no i'm not going to be covering wagtail at this point thanks for the question oh yes okay cool so what's a good way to create a lot of users like 3 500 users uh with the user and password this is a good question so i mentioned earlier the actual creation portion let's see if i can pull that up real quick and so what we've got here is product objects dot get or create so to actually create a bunch of things in there let me just jump into the shell real quick you'll go ahead and say my items and it'll be some sort of list here and what you'll do is you'll iterate over your items so for i in my items and then you'll create something like this so you create an object itself so let's i'm just using product as an example this is actually not going to work anyway but you you do something like title and then i title obviously i don't have actual data here but then you would add this to another list another thing i missed but then at the end of the day what you do is product dot objects that bulk create and then you pass in a list of objects much like that that's a really simple way to do this uh there are some downsides to it but when you have a lot of data the bulk create method is the best way to do it it's the most efficient way for the database uh but that's a good question thanks for that that's probably something i will have to have at some point as well as something on here do i have discord no not yet the python discord i haven't been on that i mean i actually let me rephrase i do have a discord do i use it on the python discord no i do not uh thanks for the question all right let's see here good question uh which days do i cover classes in 30 days of python um like i mentioned in the beginning the first 10 days somewhere in there are definitely related classes it's probably like day four or five um i don't think i do it that early on uh but but it's definitely a fundamental concept for python thanks for the question there are you gonna cover the django rest framework in this bootcamp no i'm not going to be covering it there's only one caveat is if i absolutely need it for some reason but i'm not going to be covering in this boot camp in the next 10 days thanks for the question uh but i do cover it a lot already so yeah thank you okay let's see here thank you for the comment michael yeah a lot of people don't use the django managed shell because well it takes a lot more work to work with the django managed shell um or the django managed python shell that is but i think it's absolutely critical while you're building things with django you're not in my opinion you're not actually going to be building much without using the python shell or rather the django managed python shell because this is a really fast way to prototype things and not have to call a view or a test or something like that it's just so much easier so uh thanks for that appreciate it all right so um let me know if you guys have any other questions i'm going to should be on a couple more minutes uh specifically for the boot camp itself i'm not really going to be answering questions that are outside the context of the boot camp or anything that we covered today um so yes here's a good one um i will just repeat it and answer it i'm gonna just do this real real fast because i only got a couple minutes left um so are we going to be covering model forms yes absolutely we're going to be covering django model forms um that is a critical feature to this and it's something that i'll be doing probably in like i don't know day three or day four uh next question something related to what is what are we gonna use for the front end of this one um we're not gonna be using a javascript framework at all there's just not enough time to cover building in a javascript framework in this but if you're curious about that after you do this whole bootcamp you could jump into the tweetmee2 project and absolutely get um get the bootcamp related things that are sorry get the react related things that you cover from the boot camp itself so thanks for the question uh we're gonna be using django templates in the templating engine for this uh will testing be covered in this boot camp yes absolutely great question what database we'll be using for django so something i didn't cover yet really but as one of the batteries built in to django is support for multiple kinds of databases so the vast majority of this will be covering just using sqlite database but actually using other databases really easy to do and perhaps in the next q a session i'll actually show you an example of that so yeah thanks for the question there can i write facebook api authentication for this boot camp that is probably outside the context of this series to do specifically facebook um or well rather any social you know um integration itself so uh thanks for the question uh i'm hoping that tomorrow i'll have more time for questions than today i'm gonna try and keep all of these directly into about an hour if i go any longer than that it's just gonna it's really gonna be all about um the answering questions or some for some reason the concepts themselves take a little bit longer to discuss uh so thanks for uh answering that um so can you use vs code yes absolutely that's what i was using thanks for the question um will i be covering making a mobile app in this bootcamp no i will not we'll be covering each raw html forms submitted using ajax um so i will be covering forms in detail django forms in detail it's a critical piece of this and and also using raw forms is definitely something i will be covering the ajax portion of it i'm most likely not going to be covering mainly because it's outside the context of really 10 hours worth of work and 10 hours it's actually more like i don't know six or seven hours of actually teaching the content itself uh i it's just not enough time to cover ajax there okay maybe like two more questions and then i will um i'll be hopping off hopefully i'll be able to do another live q a later today like my time so later tonight so 8 p.m uh pacific standard time i think it's gmt minus 7. so hopefully you can come out for that one and ask a lot more of these questions the next one was something related to github am i going to have a github for this absolutely i will have a github for this um so yeah do i have a discord server yet no i do not i don't have a discord server for anyone i don't have anything related to a private session yet um right now it's it's all about this live stream and getting these things going and hopefully really helping you guys out as much as possible um so yeah hey thanks for the live chat uh arjun arjun arjun is that how i pronounce your name sorry if i mispronounce your names guys i definitely don't intend any offense whatsoever uh but thank you guys thanks so much for watching um i have a lot more to cover right so a lot more and this is only day one it's probably going to be 10 days it might be longer than that it really depends on how much you guys are showing up and how things are progressing in the feedback i'm getting from you so if you're watching this after the live stream just shoot me some comments below let me know what you think of this and also if you're not subscribed how are you finding out about this i mean i'm serious about that too um obviously i'd love for you to subscribe but i don't need to hammer that in um so yeah custom form validation yes absolutely we'll be doing a lot in validation for forms and models thank you guys for coming out to day one and i will see you tomorrow hopefully take care
Info
Channel: CodingEntrepreneurs
Views: 41,712
Rating: undefined out of 5
Keywords: django, django-bootcamp, ecommerce, django3.1, python 3, web application development, python web apps, djangocfebootcamp2020oc, live coding, live stream
Id: 16x3VpF2QrA
Channel Id: undefined
Length: 62min 5sec (3725 seconds)
Published: Mon Oct 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.