Understanding Django In 30 Minutes | Eduonix

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi my name is Matthew and I'm a final year industrial engineering student I've been working with Jango for the past two years professionally in my own business and so today I'm gonna be showing you how you can learn Django in just 30 minutes and so before we get started with everything I'm just gonna give a quick breakdown of this video and what we're actually gonna go through the first thing is just an introduction to Django what it actually is all about the requirements to actually get started and then just look at a short application that touches on the code that you'll actually be using when you code with Django so the first thing to note about Django is that is a high level Python framework for web development and so if we break up that sentence what it means is that firstly Django is built using Python so if you are not familiar with the programming language Python then it would be better to start with learning that before you learn Django because a lot of the things will not make sense then the second thing is that it is a high level framework so what that means is that Django already removes a lot of the work for web developers making it a lot easier to get started and just dive in so it covers things such as admin and database management and because it allows you to just jump in and get started you actually are then being allowed to create secure and scalable applications and actually build them extremely fast and really really fast meaning within a few hours you can have a fully working application live and so by doing all of that it's really is just removing a lot of unnecessary work for web developers and this you'll see in how you can create reusable apps that you can use in more than one application so removing a lot of that work for web developers so if we look at the requirements to actually get started there's only two things that you really need the first thing is non-negotiable you actually need Python to get started so version 2.7 or 3 six and in the second thing is virtual environment which you install using Python and that is more of a recommendation than a necessity and the reason is just because virtual environment allows you to work with Python libraries inside a certain directory instead of installing them to your actual computer and so that way you can work with your projects code in its libraries in one small package instead of containing everything for all your projects in just one mess on your computer so with that being said let's jump into the actual coding application so to make sure you have everything installed if you go to Python org slash downloads you can then download Python for your operating system whether that's Windows Linux or Mac just click on whichever link is for you and then just follow the installation for Python and then once you've installed Python you can then use it to install virtual envy by just going into your command prompt or your terminal and running pup install virtual env and that will install it for you okay so with Python and virtual environment installed what you want to do is go into the terminal or command prompt and navigate into a folder that you'll be using for this project so mine is the Django in 30 directory and if I list everything out then you can see there's nothing in it it's an empty directory so this is where we're going to be working and the first thing I'm going to do is type virtual env env and what this command does is it creates a virtual environment for us which will be used to house all of the Python libraries that we'll need for this project so that our actual computer is not installing any of these libraries so now if I list everything out you can see I have the environment folder and so on Mac to activate it you'll say source in the /bin slash activate and if you're on Windows you would just type env scripts activate it does the exact same thing so then once you've done that you'll see that yet this env on the left-hand side of your working directory and that means that the virtual environment is activated so now we can actually install Django and all you have to do is set up install Django and that's going to install it into this virtual environment so that it's contained in this project and if we were to deactivate the virtual environment then Django would not be installed on our local machine so now what we can do is we can actually use Django to create a project so to do that we can say Django - admin start project and we can call this DJ 30 so now if I list everything out you can see I have this DJ 30 folder which is the actual project that we've created and so if we were to going openness into sublime text then we can start working from there okay so here in sublime text I have the Django in 30 folder open and inside there we have DJ 30 and the env folder and so the first thing I'm going to do is rename DJ 30 and I'm gonna call it SRC which is short for source and that's just telling us that inside the source folder is all of the code for this project so if you remember DJ 30 was created when we ran the Django admin start project command in the terminal and basically this is just the core of our project it's housing the settings and the URL configuration for the actual project so I'm not going to look at those files just yet but if we go into the terminal and just list everything out we're going to change directory into the source folder and if we list everything in there you can see we have the manage to PI file and so this allows me to run a few commands so the first thing I'm going to do is say python manage supply migrate and what that does is it makes migrations to the database for this project as you can see there's quite a few here and so if I go back into sublime text now you can see that it created this DBSK like three file and that is a database file sqlite3 so it's all here in binary and so you can actually specify the type of database that you want to work with mysql Postgres but SQLite 3 is the default that Django comes with and that's fine for here for local purposes so now if we want to see this running as a server we can save Python manage the high run server and that will actually start a local server on our computer at this one to seven zero zero one 8000 URL so now we can actually go into our browser and go to that URL and now you can see this page is being loaded and the sweden created this was Django this is just Django telling us the install worked successfully congratulations and this is just the page that it shows so now it actually gives us quite a few links we can go to the Django documentation their own tutorial D community and in the release notes for the latest version which is django 2.0 so before we get started with our own app i first want to mention that the django documentation is probably one of the best things about Django everything and anything you want to know about Django is well documented on their documentation page which you can go to actually from the start up of our project you can just click on this link and it will open the documentation right here so everything you want to know you can find right here but so to get started let's go into sublime text and what we're going to do is we're going to create another app which will be inside our source folder and so this is just going to be a basic app something that just shows us the basic create retrieve update delete views and so to do that we'll just create a blog post kind of app so to do that to start your own app we just say Python manage top I start app and then we call the name of the app which will just say posts so now if we list everything out you can see there's this posts folder and so if we go back into sublime text now we have posts as a folder in the same directory that has the DJ 30 folder and so whenever you run that command manage the PI Start app all your apps will go inside the directory which contains your project so every time you create an app what you want to do is go into your settings so in DJ 30 settings da what you want to do is scroll down all the way to installed apps and there you want to put the name of the app which in this case is posts and so this allows you to basically keep track of all the migrations for your app so now if we were to go and create a model inside this app then inside our settings it would take record of that app actually being there so to get started to create a model we're going to say class post and it's going to come from the Django's models dot model and this is a Bolton model that Django has created for us and we can just reference it like this so a model is just a riff a way of creating some form of data so if we think about a post a post has a title and we give all the fields the parameters for this post model in this kind of fashion so say the title is a character field and maybe it has a description and the description is a text field and we can go on and on and give all these different types of fields we can give it a timestamp and give it a date/time field we can give it an author field and so on and so on and all these fields are referenced inside Django's documentation so they are way too many to go through in just one tutorial but you can go through it here and find all the different types as well and so when specify a certain fields there are some parameters that you need to actually include so for example in a character field you need to specify the maximum length and so we'll just say 120 characters is the maximum length for this for the title field of any post that you create and then just to help with the representation of the model we can say define string and that takes in self and then we just say return self title so every time you see this post in the admin it's going to show you the name of the title of the post as a way of referencing to it so every time you create a model inside your models dot pie you need to make migrations to it so I'm just going to clear everything here and we're going to say Python manage supply make migrations and so now you can see it says your migrations for posts and it creates this actual migration file inside our migrations folder inside of the posts app so now you can see posts migrations and then this 0 0 0 1 initial here it's creating the fields for the post model that we just created inside this model's file and it's giving it an ID a title and a description and so this is basically jenga's way of creating the actual models in the database and so every time you make migrations you then need to complete those migrations by saying migrate and so now you see it says applying those migrations so you have to do this every single time you make some kind of model in your models pie and so let's go ahead and run the server now so we'll say python manage the pile run server and what i want to introduce you to now is the admin so if you go to the normal URL for this Django project and you type in slash admin and it takes you to this login form but we don't actually have an admin user yet so we need to create that so exit out of the server and I'm gonna clear again and we're gonna say Python manage the PI create super user so that's gonna create an admin kind of user and so we'll say the username is admin I'll leave my email address empty and we'll enter a password which just has to have eight characters and I will say manage supply run server go back in the air will refresh the server and we'll type in our username and our password and then login and look at this this is the actual Django admin URL that we're actually loading here this is a view that Django has created we didn't do this this is literally just Django giving us this this back-end administration site for us and this is also one of the highlights of Django why it's so amazing because this was all done for us so this is actually a place where we can take record and actually monitor all the add all the models that we've created so if we go back into sublime text and going to posts admin dot PI we can register all the models that we've created so I'm going to import the model we just created and I'm going to say from dot models import post and then we can register register this model by saying admin dot sight dog register post and now if we just refresh this now you can see that we have the posts model which is showing you and we can actually go to it and add a post and now it shows us those fields title and description and we can say the title is this is our first post and this is some content and we click Save and there we go there's our first post that we're actually creating so with all of that done now we can actually start looking at how to view the starter and Django makes it really easy it gives us this views dog pie file which is inside our posts app so if you go into fuse dot pi we're going to create our first view that will show that data and so as part of the crud views which is create retrieve update delete one of those views is the actual list view and that's not included in the crud statement but it is a kind of retrieve view so we want to list all the posts in a view and so to do this we'll just say define post list view and Django allows us to create these things called function based views and so a function based view takes in a request and then it shows dot in some kind of way so the way I'm going to do it is I'm going to say return because every function has to return something and we're going to render that request and when we use the render function it takes in a request and it takes in some template and it takes in some context and the context is a dictionary that has something inside it so what do we want to show you we want to show all the posts so first we actually have to get the post object so we say from dot models import post and now we use Django's query set abilities which allows us to filter all the models of a certain kind and so we can do this in the following way we can save post objects equals two and then we filter the post model so we said post objects all so this query set is getting all the post objects just as simple as that and then we have to reference this object inside our context so how we do that as we say the post objects in these abbreviations here is this is the key value or the key and this is the value so the post objects value this first here is being referenced with this key being post objects and then we passed in this context dictionary into the render and you'll see how this works in a second but first we have to specify a template name which will actually be that HTML that shows this data so I'm going to say index dot HTML and then inside posts I'm going to create a new folder and I'm going to say templates and then inside templates we create another new folder and call that posts and then inside posts we create another new file and I'm going to save that as index dot HTML and so to start that I'm just going to create some basic HTML and then in here let's say we'll put an HTML and an h1 tag and we'll say this is our list of posts and so back in the views I'm actually just going to change this to become posts slash index dot HTML because that is referencing this directory inside the posts app so it's saying inside posts look for index dot HTML and then the last thing we need to do is actually actually show this view and reference it to a URL so inside the DJ xxx folder inside URL spy I'm going to delete all those comments and what I'm going to do is import the view that we just created so this post list view inside URLs I'm going to say from posts views import post list view and so here we're going to create a path and this path is to a certain URL so I'm going to say path and we'll say posts and then a comma and then the name of the view function that will be used at this URL request so basically when someone goes to the one to seven point zero zero one eight thousand slash posts it's going to redirect that or not redirect but take that user take its request and post it through to this post list view and that's going to go straight to this view function where it will then grab all the post objects pass in the data into a context object and then render the data to this index dot HTML file which is this right here so right now there's no way of us actually showing the data but we can do that by actually referencing the context variable and that is this post objects so if we go in here inside double curly brackets we can pass that in and just say post objects and now that list will actually show there so if we go back to the server we can see it's still working everything's perfect and now we just have to go to one to seven slaps laughs posts and there we go and now we see here this is our list of posts and it's showing us a query set with this is our first post inside it and so what we'll actually want to do want to do with this list is loop through it so if you're familiar with pattern then you should be familiar with for loops and so the way we use for loops in Django's smart templates is by saying for post in post objects so again we're referencing that context variable and then we just say post and then end for and I'm going to delete this line over there and so let's go back inside here and recheck that and now there we go this is our first post so again I'm gonna go back into the admin and we'll create another one and we'll say this is our second post title and this is some context and we'll just save that now if we go back in here and refresh this then they were showing it this is our second post title so we can actually just a little bit better and put this in P tags and now if we come back here refresh there we go so now this is actually listing out all our data so if we just recap through everything we just did we've got the model the model is describing what kind of data we want to show and in this case it's a post and the post has certain information a title and a description and you can put whatever you want here you could put an author you could put a timestamp so like a the date you actually created it you could put pretty much anything you want so I'm just gonna comment these out and then in the admin we registered this actual model we imported it with a relative import so inside posts add this admin file we're using relative imports because the models file is inside the same folder as the admin file and then we're just calling admin dot site on register and then inside the views we're creating this function based view which takes in a request whenever someone goes to that URL then we our query setting the actual object so we're grabbing in this instance we're grabbing all of the post objects and then we're passing this object as the value for the key inside this context dictionary which we pass into the render function and this render function will always just take the request the name of the HTML file and the context for it and then lastly the URL is telling is is basically telling Django to which URL path the user must go to in order to actually display the data according to a certain view so when you go to slash admin it's using the admin site URLs to show us that actual admin site and then when you go to slash posts its using the post list view which we created to show you the data on this index.html file and then inside index dot HTML we are looping the post objects context variable which we specified in the view over here and we are just using simple Python syntax with a for loop and in just specifying end for and then creating a paragraph tag with post being the incremented value through the post objects list and that is what's giving us this display over here and so that is the basics of Django obviously there is a lot that could not be covered in this but the basic premise is the idea of creating models which houses the data for a project and in displaying that with a view which is referenced via some path or some URL that you need to go to on our website and then is rendered with HTML using Django smart HTML templates so if you like this tutorial then please subscribe down below and give us a like and thank you for watching [Music]
Info
Channel: Eduonix Learning Solutions
Views: 69,117
Rating: 4.9296751 out of 5
Keywords: django, python django, python, django tutorial, django unchained, eduonix learning solutions, eduonix, learn django in 30 minutes, django windows tutorial, django forum example, django course, best django tutorial, wsgi python tutorial, wsgi python, wsgi django, sql interface django, django routing, python web app, python web application, python web application tutorial
Id: IMG4r03G6g8
Channel Id: undefined
Length: 25min 41sec (1541 seconds)
Published: Fri Jul 06 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.