Learn Django in 20 Minutes!!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'll show you how to build a simple Django application as fast as possible afterwards you'll know how to set up a Django project how to configure URLs how to create a simple database model and how to display Dynamic data using templates in case you aren't aware Django is a powerful web framework for python that's been used to build websites like Instagram Spotify and Dropbox with that said let's get started after a quick word from the sponsor of this video I've got some great news for you guys recently I've got to know a fantastic team who created a free tool that can supercharge your development Journey especially if you work with Django now this company is called app tension and they've sponsored this video so that I can tell you about their SAS boilerplate picture this you've got a brilliant idea but you're dreading the hours or weeks of coding that lies ahead just to get the project set up now that's where SAS boilerplate comes in this is not just a fancy name this is a tool that literally removes all of that tedious work and lets you dive in right to the heart of your project now this comprehensive of Kit covers everything your SAS application needs front end back end API scalable AWS based architecture an intuitive admin panel workers it's all there it even includes essential ready-to-use features like authentication notifications and yes even payment and subscription systems with a stripe integration and for you content Wizards out there there's a CMS integration with content full what's more the team behind this actually has a dedicated Discord community that you can join to get any assistance that you need so why spend the weeks setting up your code base when you could be working on the core product and building features that provide users real value check out SAS boilerplate from the link in the description and get started today skipping through all of those tedious setup steps alright so let's begin here by setting up Django to do that we're going to open some kind of directory you can see I have this demo directory opened in Visual Studio code and we're going to get into a terminal environment I just opened the terminal in vs code obviously you need python installed and once you've installed python we're going to type pip install and then Django in our terminal to install the Django package now if you're on Mac or Linux you can type pip3 install Django and once Django is installed we can create a Django project now restart your terminal if you're working in the same one and then type the command Django admin start project the name of the project which in this case can be something like demo when you do that you'll see that it will generate a new directory for you that contains a bunch of files that are pre-generated by Django let me walk through what these files do and then we can create a Django application so the first file we have here is a knit.pi now a knit dot Pi is a special file that tells python to treat this directory like a python package next we have asgi and wsgi these are special configuration files that we don't need to deal with these are going to allow Django to actually communicate with the web server we then have a settings.pi file straightforward this contains a bunch of different settings we'll go in here quite often when we need to install different Django applications install plugins change some of our middleware and do things like modify our database engines if you're not aware Django is compatible with many different databases and you can configure all of that type of stuff inside of this settings file we then have a urls.pi file this file will allow us to configure different URL routes that we can then kind of route or direct to different Django applications which I'll get into in one second next we have a manage.pi file this is a special file again that actually acts as a command line tool that allows us to run special commands to do things like make database migrations run our python server and all kinds of other things like creating users for our Django admin panel which we'll get into in one minute alright so now let's quickly talk about Django applications so right now we have something known as a Django project the Django project has these main files that I just discussed but if we actually want to have any executable code or be able to see a kind of website appearing we need to create something known as a Django app now a Django app is meant to be a standalone application that you can plug and play meaning I can take it out of this Django project and put it into another Django project these apps contain things like database models different views or routes templates all kinds of other stuff we can have inside of our applications so what we need to do whenever we're working with Django is create an app so to make an app what we're going to do is go to our terminal we're going to CD into the directory where we have our now Django project which is called demo and we're going to run the command Python manage.pi and then create app and actually that's going to be start app then we're going to put the name of the app in this case I can just do something like my app when I do this is going to create an application inside of here called my app which contains a bunch of different files which we're going to work in in just one second now once we've created this application we actually need to link this to our Django project to do that we need to go into this main folder which is the same name of our project go to settings.pi so scroll down to where we see installed applications and then place a string containing the name of our new app in this case it's going to be my app this will essentially install the application and allow our Django project to now view any of the code we put in this app now just to clarify apps a little bit more we can have different apps for different purposes so we may have an app for authentication we may have an app for specific user types so maybe admins or moderators we may have an app that allows us to view the main content of our website there's all kinds of different applications we can create and we can separate different logic into different applications where it makes sense to do so now that we've created this app what we want to do is go into the app and start creating some simple URLs and some Roots so first of all let's just have a look at some of the files we have inside of here so again a knit.pi we've talked about that admin.pi this allows us to register database models so we can view them on our admin panel we have apps.pi which we don't need to worry about models.pi where we'll place our database models tests where we can write some automated test cases and then views which is mainly where we'll work where we will create different views or routes that we can access on our website for now now though I actually want to create a new file here called urls.pi This is where we'll Place different URL routes and then connect them to our views so let's go into views here and create a simple view just so we can test how things work so to create a view or a route what we're going to do is type Define so we're going to create a function we'll give this a name something like home we're going to take in the request object as a parameter which will allow us to access things like query parameters and the body of different requests that are being sent to this function and then from here we're going to return some type of response now we can render some HTML templates which I'm going to show you in one second or we can actually return an HTTP response so I'm going to import at the top of my program here HTTP response I'm going to go return HTTP response like that and inside of here I'm just going to return a string which is Hello World this just allows us to return some very simple data which we can then display on the website so now that we have this view which is simply a function that returns some kind of response we need to actually connect this to our application through a root or a URL to do that we'll go to this newly created urls.pi file inside of our application notice we have one here and we have one inside of our project now inside of here what we need to do is create some URLs so we're going to say from Django dot URLs import path we're then going to say from dot import views and we're going to import this views.pi file that we have here now we're going to specify a variable which is URL patterns we're going to make this equal to a list and inside of here we're going to specify paths that will connect a URL pattern to a specific path or view so for now I'm going to put an empty path which just means we go to the root kind of URL of our website I shouldn't have said root kind of the base URL of our website we're going to connect this to the Views dot home view or function then we're going to specify that this has a name of Home essentially what this means is when we go to this kind of empty string path here we're going to call the views.home function which is this view which will then return this HTTP response which will allow allow us to view that okay so that's great we've now configured the URL within our application however we also need to configure the URL to our application I know this seems a bit weird but let's go inside of demo now go to urls.pi and we're going to create a URL root that allows us to connect to our application so here inside of demo we have all of the base routes or URLs for our entire project we then need to kind of create a URL here that will link into our specific applications you'll see why this is important in one second but for now let's create that link so we're going to say path we're going to put an empty string here and then we're going to use this function include which we'll import from right here inside of include we're going to specify the name of our application which actually in this case is my app Dot and then URLs now all this is going to say is that whatever I go to this empty string I want to forward all of the different URLs or Roots into myapps.urls where they will then be handled here so to give you a better example let's imagine I did something like my app slash if I did that now what would happen is whenever I go to my app slash in my URL bar or my address bar it would then take the remaining part of this URL and forward that into this root right here meaning if I wanted to access my home page I would now need to go to my website domain and then my app slash like that if I go there then anything after this slash is going to be passed into this URLs file where it will then be handled here meaning if I had something like home here then what I would need to do to access that route is type my app slash home so the home component so then next kind of part of the route will be handled here whereas the main part will be handled by this main application now this is cool because this allows us to have different prefixes for our different applications and have similar URLs within different applications that can still be accessed because we have this main prefix hopefully that makes sense but for now we're just going to make this an empty string we're going to remove this and we'll go back here and make this an empty string as well so we're able to view this root now that we've done this let's run our application and make sure all of this works then move on to some templates and databases so to do this we're going to type python manage.pi and then run server now when we run the server here you can see that we're getting an error don't worry about that we'll fix that in one second for now I'm going to look at the URL here which is the URL that is running our server so localhost Port 8000 I'm going to press on control and then press that I also could just type that into my address bar and you'll see that I get Hello World appearing on my screen okay all of that is great and to stop the server I'm going to hit Ctrl C on my keyboard now what I want to do is show you how we can use something known as a template so a template is essentially a reusable HTML file that allows us to display Dynamic data now we can have templates that inherit from other templates I'm going to quickly show you how that works so to do that what we need to do is create a templates folder inside of our application so I'm going to go to my app I'm going to create a folder called templates very important that you name it templates if you don't name it templates this won't work inside of templates we're going to create a new file called base.html inside of here we can use something known as the Jenga templating engine which allows us to display Dynamic data now I'm just going to paste in the template here because I don't want to write all of it out in this video considering we're trying to go fast now what this is is a simple HTML document that has a bootstrap nav bar and I just want to show you how the template works now inside of these templates we can create things known as blocks these blocks are overrideable pieces of content so I have a block title I'm ending the block here meaning I can then override this title in another template continuing I go down here and I have block content and N block meaning I can inherit from this template get all of the code that's inside of here so I get the nav bar and then I can change whatever's inside of these different blocks to illustrate that let's make another template here called home.html inside of home we're going to paste in some template code here so let me just grab this and finish this off so type end block here we're going to put a paragraph tag saying this is the home page and then end our paragraph tag now what this does is extend the base.html template meaning all of the code that exists inside of base.html we're going to import here so we can have a consistent style for all of the different pages in our website without having to rewrite all of that code we're then going to override the content that exists inside of our blocks so for the block title we're going to put home page for our content block we're simply going to put a paragraph tag that says this is the home page that's great we now have our templates now that we've done that we simply need to render the template to render the template we're going to go to our views file inside of our application and we're going to use this render function for the render function we're going to pass request which is right here as the first argument we're then going to pass the name of our template which is home.html again this only works if you have it inside of the templates directory now that we've done that we can rerun our server and we can view this template so let's run that again ignore the error for now and you can see that when I have a look at my website I get this kind of nav bar appearing and then I get this is the home page inside of my content block now that we've done that let's have a quick look at databases all right so I've opened up this models.pi file inside of my my app application what we're going to do now is create a database model that we can then access and use from Django now Django is great because it provides something known as an orm which is an object relational mapping this means that we can write python code to create different database models and then have whatever models we create be automatically made for us in some kind of structured database schema like sqlite3 so you'll actually see that as we start creating these models what will happen is we'll make something known as a migration this migration is actually automated code that will then go and create the corresponding model in something like SQL mongodb or whatever it is that we're using as our database backend engine you don't need to understand this fully but for now I'm going to paste in a symbol date database model you can see that I have a to do item this is a model and we have different fields on the model which are written as python attributes we have a character field in a Boolean field obviously we can get much more complicated and do things like references but I don't have time for that in this video so we've created a simple database model what I need to do now is register this model with my admin panel which we're going to look at in one minute and then I need to apply something known as a migration so that we actually have this model existing in our SQL Lite 3 database so what I'm going to do here is go to admin.pi admin.pi is the file where we can register different models so that they will appear inside of our admin panel allowing us to modify and view them so what I'm going to do is type from dot models import to do item which is the model that we created you can import other models here as well we're going to type admin Dot site dot register and then we're going to put the to do item here now when we run our application and we go to the admin panel which we're going to do in one second we'll be able to view this model okay now that we've done that what we need to do is make our migrations so anytime you make a change to your database models you need to make something known as a migration now this migration again is some automated code which Django will apply to the database which allows you to change your models and update them while kind of maintaining that data and ensuring that if data already exists in the database you're not going to break that or remove that when you make a change to the database schema the way you do this is you type Python manage.pi and then make migrations you need to run this any single time you make a change to any of your database models after that you're going to type python manage.pi and then migrates if I can spell this correctly now when you type migrate this is going to actually apply the migrations and update the database for you so now you'll see when we rerun our application we'll no longer get that error because we've made the correct migrations here to our database so this is something that a lot of people get confused with pretty much anytime you make a change here so you add another attribute you add another model Etc run make migrations apply the migrations and then Django will automatically handle all of the operations on your database so that you don't need to deal with them yourself alright so now that we have this database model let's see how we can do some operations with it so what I'm going to do now is go in and create a new view now what this view is going to do is render a template that will view all of the different to-do list items that we have so actually let's first go and make a template I'm going to call this to Do's dot HTML inside of here I'm going to paste something feel free to pause the video and have a look at it but I will briefly explain so inside of here you can see that we're extending from the base template we're overriding the block content and we're actually using a for Loop within this template which is something that is valid what we do here is we render different list items for every single entry in our to-do's which is a variable we can pass into this template then we can access an attribute on this variable which is a python dictionary we do that using this double curly braces anything that you embed inside of Double curly braces here will be treated as a variable that you are kind of extracting and viewing as the actual value rather than some HTML content so whenever you're using a variable use two sets of curly braces then what we're doing is running an if statement we're saying if the to-do list item is completed render completed otherwise render not completed then we are ending the for loop block and we are ending the content block there's a lot of other cool stuff you can do here but this is the basics on conditional rendering and doing kind of block rendering or multiple rendering based on some type of variable you pass inside of here so now what we need to do is go reviews and we need to create a view that renders that template so we're going to say Define to Do's we're going to take in our request and we're going to return a render we are going to render the request if we could type this correctly this is going to be to dos.html and now what we need to do is pass a python dictionary that contains the variables or the key mapping pairs that we want to view inside of here so in this case I want to pass to Do's so what I'm going to do is put a dictionary I'm going to say to do's and then I want to view all of my to-do list items now how do I view of my to-do list items well I need to query them from my database fortunately since we have the orm we can do that quite quickly so we can say from dot models Imports to do item and then we can access all of the instances of our to do item so to do that I'll say to Do's or actually I can't do that because that's the variable I'll just say items is equal to to do item dot objects dot all this is going to get all of the different objects that exist inside of this database field then I can simply pass them here as a list object so I'll say to Do's is equal to items now I'm successfully rendering this template now what I need to do is create a URL for this template or for this root to do that I'm going to go to URLs and I'm going to create a path for the path this time we'll put to Do's slash like that then we're going to say views dot to do's and we'll say the name is to Do's okay so now assuming everything is successful we should be able to go to the do's and view all of our different to-do's although we don't currently have any so if I go here two slash to Do's you can see that we get a to-do list header but it doesn't contain any items now we're going to quickly look at the Django admin panel and then you will see how we can view those different items alright so now we're talking about the Django admin panel now this is a special thing that's provided by Django which allows us to manage users and different database models it's quite useful and requires minimal configuration what we need to do if we want to work with the Django admin panel is create a user so we're going to type Python manage.pi and then create super user when we do that it's going to prompt us for a username and password we don't need to do the email I'll just type in a password Here make sure you remember it once you create this user then what you can do is run your application so python manage.pyrun server you can go into your main URL here so we have that open and then type slash admin when you type slash admin it will bring you to a pre-built admin dashboard where you can then sign in with your credentials so I'll type in my password here and be brought to this page now notice it shows me the different database models from my various apps we also have groups and users which we're not looking at now we have access to to do items so I'll go to to do items and I can press add now I can actually modify my different items so I can do something like my item and Mark that as completed let's do one more another item and Mark that as not completed now that we've done that we can go back to slash to do's and you'll see that we have the two different to-do list items appearing and those will be saved persistently inside of our SQL Lite database which is running local locally sorry but we could actually host and kind of have you know a more persistent database that's not just a file on our computer alright so with that said I'm going to wrap up the video here this was the fastest I could possibly show you Django without skipping over a ton of details now obviously there is a bunch more stuff to learn if you'd like to learn that I have entire comprehensive tutorials on this channel and actually an entire Django course that is completely free so feel free to check that out with that said if you enjoyed the video make sure to leave a like subscribe to the channel and I look forward to seeing you in another one [Music]
Info
Channel: Tech With Tim
Views: 309,299
Rating: undefined out of 5
Keywords: tech with tim, learn django, django windows tutorial, best django tutorial, django unchained, python django tutorial, django course, django tutorial for beginners, django crash course, django routing, django tutorial, eduonix learning solutions, django crash course 2021, python django, django forum example, introduction to django, wsgi django, django, dennis ivanov, python web application tutorial, software developer, python programming, learn django in 30 minutes
Id: nGIg40xs9e4
Channel Id: undefined
Length: 21min 24sec (1284 seconds)
Published: Sat Jul 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.