Python Django Tutorial: Full-Featured Web App Part 1 - Getting Started

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there how's it going everybody in this series of videos were going to be learning how to build a full-featured web application using the Django framework and Python so Django is a very popular framework that gives us a lot of functionality right out of the box and makes it really enjoyable to work with these web applications so first let me show you what will be building in this series of videos and then we'll get started learning how to actually put all of this together so this is the application that we'll be building here and you can see that it's a blog style of an application where different users can write different posts now this can be like blog post or they can be Twitter updates or whatever it is that you want to do with this now we have an authentication system so I'm logged in right now so we can log out and if we go to register then new users can create a new account if you already have an account then you can login and we can see at the login page we have this forgot password link now that will allow users to reset their password by getting an email now if we have an account then we can login so I'll say Corre EMS here is the username I created and I'll login with my password and now that we're logged in we have a few different options up here at the top so we can view our profile and update our profile information now we can also update the profile picture that we have here so if I go to choose file and choose a different file and update that then we can see that profile picture was updated now that's also resized in the background to save room on our web server if that picture is too large so if we go back to our home page then we can view other people's posts so we can view an individual post here by someone else now if we view an individual post that is something that we've written then we also have the ability to update or delete that post so if I go to update and say my latest updated post and post that then we can see that now that post was updated with that new content so we also have the ability to delete posts if I click on delete then we'll ask if we want to confirm that we want to delete the post so I'll say yes and then back on the home page we can see that that post was deleted so that's a quick tour of the application that we're going to be building and building something like this is a great way to learn the ins and outs of a framework because you're going to be exposed to so many different things so for example we'll learn how to work with databases and also how to create an authentication system and accept user input from forms and send email to reset passwords and all kinds of different things like that now since this is a django application we also have the ability to access an admin page if we have the correct permissions and within here you get a nice GUI to be able to view all of this back-end information and update it on the fly if you'd like so let me go back to the main site here so we're going to be learning how to add a lot of functionality to an application within this series now I'm gonna mention this several times throughout the series but if you're following along and would like to download the source code of each step in the process then I will have links to the source code of each video in the description section below so that you can download those if you'd like and also if you'd like to know how to build the same application and another framework then I also have a flash series where I've created the same application using the flask framework so if you're interested in that then I'll put a link to that in the description section below as well also I'll be doing a video in the near future where I compare the two frameworks and when it might be best to use one over the other okay so let's get started with learning how to build this application using Django so I'm going to close down my browser here and pull up my terminal and if you're on Windows you can pull up your command line so first off let's start off by installing the packages that we need to get started out so you can do this in a virtual environment or in your default Python environment but it's always a good idea to separate different projects into their own virtual environments now I'm mainly going to focus on Django in this series so if you need to install Python or want to learn how to work with virtual environments or are wondering how I set up my text editor or anything like that then I'm gonna put links to those videos in the description section below but I'm not gonna go into those and detail in this series I'm just going to assume that you're specifically ready to start learning Django okay so with that said let's get started so first let's install Django so to do this we can simply do a pip install so I'll say pip install Django and we can see it looks like it's successfully installed Django 2.1 but to be sure let's run a command and that command is python - m django - - version and that should display the version of django that we are using so if that runs then django was installed correctly now you can also see that we're using django version 2.1 in this series so be sure that you're also using Django to point 1 or higher or else some of what you learned here might not work in previous versions now I'm also using Python 3.7 so you'll want to use a later version of Python also if you can there are some features that I'll be using in this series such as f strings that are only available if you're running Python 3 point 6 or higher ok so now we have Django installed so now let's create a new project from scratch so I'm on my desktop here you can create this project anywhere you'd like on your machine but I'm going to use my desktop so to create a new project we're going to use some commands that are available to us now that Django is installed and one of these commands is Django admin so if you type that in then it should show you the available subcommands so if I run that that is Django - admin then we can see that we get a list of subcommands here and we can see that there are a lot of different sub commands listed here and we'll see a couple of these later in the video but we're going to use the one right now called start project which is right here so start project will create a new Django project for us that has a complete structure with different files and everything else that we'll need to get started so let's do that so I'm simply I'll clear my screen here and I'm simply gonna say Django - admin start project and now we can specify the name of our project so I'm just gonna call mine Django let's do Django - project and run that ok so that's not a valid project name let's see if I use an underscore instead ok so that worked I always forget which one is which so you can create a project with an underscore but not a dash so I created a new project called Django underscore project and now that we've created that if I look at my desktop then we can see that now I have a directory on here called Django underscore project ok so first let's simply CD into this directory and look at the project structure of what that start project command just created for us so within my command line here I'm gonna say CD into Django underscore project and now I'm just going to open this project and a text editor now I'm using sublime text but you can use any editor that you'd like I have a video on how I set up my editor and that will be in the description and below if anyone is interested so I'm going to open up sublime text here and then drag our Django project into sublime text and open that up so now let's look at the project structure that that start project command created for us now I don't know how to make the text over here in the sidebar of sublime text any bigger so instead I'm gonna use my terminal to look at this so let me open the terminal back up here and clear the screen now on my machine I have a command called tree that helps visualize this a little bit better so I'm going to use that now you might not have this command installed on your machine but you could simply use your editor to view this structure instead so I'm gonna use that tree command okay so we can see that we have a pretty simple structure here now I'm going to open up my editor and go through each of these really quick so just so we can still see the terminal and the structure here I'm going to put this over here on the side about right there and my editor I'm going to drag over here and make this take up about 3/4 of the screen here okay so we can see here in the structure that on the base level we have a manage pie file and a django project directory and that man is not pi file is a file that allows us to run command line commands so let me open that up here in my editor and this is what that manage dot pi file looks like now we'll see this in action in just a second when we run this default site but we won't actually be making any changes to this file so also in our base directory for our project we also have a directory called django project which is also the name that we used for our project itself and within that directory let me open that so within this directory we can see that we have four different files the first is this double underscore Anette dot pi and if we look at that in our text editor then we can see that that is just an empty file so that just tells python that this is a Python package so next we have this settings dot PI file so if I open that in the editor as you can probably tell from the name this is where we'll change different settings and configurations so we'll be using this throughout the series we can see that most of these files have good documentation and links provided where we can learn more information now if we get glanced through here then we can see up here towards the top that we have a secret key and that just adds a lot of security enhancements to Django now we can see that we also have debug set to true here we have an installed app section we have some database settings here and all kinds of different useful settings that we will talk about more in future videos but for now we're just taking a quick glance so looking back at our project structure we also have this URL spy file so let me open that up now this is where we'll set up the mapping from certain URLs to where we sent the user so for example there is one path pattern here set up right now and that is for admin so if we were to go to our site and go to the route admin then it will send us to this admin dot sites not URLs and we'll see more about this in just a moment when we add some additional routes okay lastly we can see that we have this whiskey dot pie file here if I open this up in the editor so whiskey or WSGI is how our Python web application and the web server communicate so Django set up a simple default whiskey configuration for us in this file but we're not going to be actually touching this file okay so that is a quick overview of the project structure that was created for us when we ran that start project command so now let's actually open up the default web site in our browser unlike a framework like flask we actually don't have to write a single line of code to open up a basic web web site in our browser we just have to run a command so to do that we're just going to use that manage py file so to run our server that to access our site we're just going to go over to our terminal and I'll make this larger again so that we can see everything and now within that Django project in the same directory where that manage py file is we can say Python manage py run server and that is a command that we're going to be using a lot okay so you're likely going to see this warning here that says that you have 15 unapplied migrations and then it tells you a command that you can run to get rid of that warning so let's not do that right now I'll explain this migration stuff in just a bit but for now if we look here at the bottom it says that our website is now running and that we can access our site by going to this URL here HTTP 127 0 to 0.1 port 8000 now another name for 127 0 dot 0 dot 1 is localhost it's basically just our local computer that we're currently on now this is actually a running webserver so we have to leave this running while you're viewing your site in the browser or else you won't be able to see it so let's copy and paste this URL that it gives us and open this up in the browser so I'm going to open a new incognito window here and paste that in to our URL bar and we can see that we have what looks to be a website here so this is the default website that Django has created for us and in this series we will modify this so that we have the web application that I showed at the beginning of the video so by default it shows us that a page that says that debug is equal to true and also it provides us some links down here to the Django documentation and stuff like that now when I said that 127 zero to 0.1 was the IP address of our local machine I also mentioned that there's an alias for that IP address called localhost and I like using that more than the IP address itself so if I replace that 127 does zero to 0.1 with localhost then this should still work so now we're at localhost port 8000 and we still have that same site ok so if you remember I said that within our projects URLs dot PI module that we had that admin route so let's try to navigate to that route really quick so if I go to Ford slash admin then we can see that there's actually something on this page we get a login screen so let's pull back up the project and open that URLs dot PI module one more time here so like I said before this is a way to map URLs to certain locations so that they can be handled in a certain way so in this case when we went to ford slash admin then it's going to admin site stat URLs and then the logic within that location handles the route further and we'll look at adding more routes but for now that is how that admin route is working so the logic at this admin dot site URLs that is what allows this admin page here to work correctly now we can't access this with any credentials just yet because there is some more that we need to do first but I just wanted to show you that the URLs and that URLs module are already routing us to different locations and in the next video we'll see how to add some more routes to that module so that we can show users exactly what we want them to see when navigating to different areas of the site but for now let's open our terminal back up and stop running our server so I will pull up my terminal and you can stop running your server by hitting ctrl C and sometimes I have to hit that twice to get that to stop for some reason but just hit ctrl C a couple of times and it should stop now I will clear our page now with that server running and debug mode like it was it should automatically reload any changes that we make to our code but if you ever have any trouble with the website not reloading for any reason then you might want to stop the server just like we did and restart it by running the command that we reran earlier so to bring it back up we can just do python manage py run server and that runs the server again ok so I think that's going to do it for this video in this video we learned how to get Django installed and how to create a new project we also looked at the structure of what gets created and how to pull up that default site in the browser so in the next video we'll learn how to create an application for our site and set up some basic routes now that might sound confusing right now that we'll be creating an application within our current project but we'll see what that means in the next video but if you have any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest ways to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page and in description section below be sure to subscribe for future videos and thank you all for watching you you
Info
Channel: Corey Schafer
Views: 1,516,611
Rating: 4.9798994 out of 5
Keywords: python, django, django 2, django 2.0, django 2.1, django version 2, python django 2, django tutorial, python django tutorial, django install, django tutorial for beginners, django getting started, django introduction, django hello world, corey schafer, python programming
Id: UmljXZIypDc
Channel Id: undefined
Length: 15min 32sec (932 seconds)
Published: Fri Aug 31 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.