Introduction & Application Demo
Hey there, how's it going everybody? In this series of videos, we're 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 we'll 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 posts 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 CoreyMS here is the username I created and I'll log in 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 going to 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 Flask 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.
Environment Setup & Django Installation
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 going to put links to those videos in the description section below, but I'm not going to go into those in 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 2.1 or higher or else some of what you learn 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.6 or higher.
Creating a New Django Project
Okay, 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 subcommands 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 startproject which is right here. So startproject 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 going to say django-admin startproject and now we can specify the name of our project. So I'm just going to call mine django_project. And run that. Okay, so that's not a valid project name. Let's see if I use an underscore instead. Okay, 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_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_project. Okay, so first let's simply cd into this directory and look at the project structure of what that startproject command just created for us.
Exploring the Project Structure
So within my command line here, I'm going to say cd into django_project. And now I'm just going to open this project in 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 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 startproject 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 going to 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 going to 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 three-quarters of the screen here.
Okay, so we can see here in the structure that on the base level we have a manage.py file and a django_project directory. And that manage.py 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.py 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 __init__.py, 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.py 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 glance 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 urls.py file, so let me open that up. Now this is where we'll set up the mapping from certain URLs to where we send 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.site.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 wsgi.py file here if I open this up in the editor. So WSGI is how our Python web application and the web server communicate. So Django set up a simple default WSGI 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 startproject command.
Running the Development Server
So now let's actually open up the default website 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 website 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 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 runserver. 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.0.1:8000. Now another name for 127.0.0.1 is localhost. It's basically just our local computer that we're currently on. Now, this is actually a running web server, 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 into 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 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.0.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.0.0.1 with localhost, then this should still work. So now we're at localhost port 8000 and we still have that same site.
The Admin Route & Next Steps
Okay, so if you remember, I said that within our project's urls.py module that we had that admin route. So let's try to navigate to that route really quick. So if I go to /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.py 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 /admin, then it's going to admin.site.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.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 in 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 in 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 ran earlier. So to bring it back up, we can just do python manage.py runserver, and that runs the server again.
Okay, 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 way is 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 in the description section below. Be sure to subscribe for future videos and thank you all for watching.