Course overview and application plan
Hey guys, welcome to this tutorial series on Django. In this series I'm gonna be teaching you how we can use Django to build out a fully functional website. We'll cover things like rendering your first page, setting up a database, adding items to that database, and rendering those items out on a web page.
The application I decided to build for this course is going to be a customer management platform. This application stores customer information in a database, kind of like a CRM, like Zoho. Users will have the ability to create customers and customer orders along with viewing those customers and updating customer information like orders and basic contact information on a customer's profile page.
We'll also have the ability to search customer information using a multi-parameter search form. As I release a series I'll be adding things like login authentication, password resetting, and whatever else you guys recommend in the comment section. If I don't add it to the series I'll do my best to take your recommendations and make them in a video series separate from this course, so make sure you're subscribed to my channel so you can see those when I release them.
So what is Django? To summarize it briefly, Django is basically a way we can create websites using Python. It's a very popular web framework that makes putting together websites much easier rather than writing out all that Python code yourself. So let's get started by installing Django and getting a basic project setup. I'll quickly go over the file structure and explain the parts that make Django work.
Installing Django and creating a project
The first thing we'll need is to install Python because we are downloading Django using the pip command. So I'm in the directory where I want to host my project and I'll just go ahead and do pip install django and I already have it so it's going to tell me that I have all the proper packages, but go ahead and let the install run. Once that's complete, to make sure it installed properly go ahead and run django-admin and then just hit enter.
If you see this it means that Django installed correctly and you now have access to all the commands that Django provides us now. The one we're gonna work with is this startproject command right here. This command gives us the ability to start our first Django application and long boilerplate files. So from the directory again where you want your project, go ahead and run django-admin and use the startproject command.
At this point all we need to do is give our project a name, so I'll just go ahead and call mine CRM and then I believe I already have one called CRM so I'll just do CRM1. What this just did is in my directory, so on my desktop it just created the boilerplate files for me. So here's CRM1 and I'll just drag that out there and I'll go ahead and tap into that directory. So let's go ahead and change this to CRM1. Okay, so we're now in that project and I'll go ahead and use Sublime Text, a text editor here, and I'll open up the files that we just have created for us CRM1 and we'll go ahead and take a look inside here. I'll drag this out.
Explaining the generated project files
Let's take a quick look inside all these project files and I'll just give you a brief overview. So the first one we're seeing here is this manage.py file. You never want to touch this file. This is a file that gives us access to a list of commands that we're gonna use very frequently, and you'll see, so we never want to touch it—just leave it alone.
Then in this folder right here, so I created this structure CRM1 and then a folder inside in our root directory we have this WSGI file and this is basically a web server that Django sets up for us and we're also not going to touch it now but we'll just leave it alone for now.
Then we have this urls.py file and this is basically just a URL routing system. So all it is is just a list here and we're gonna add a list of paths. So let's say mywebsite.com/ customer and when you type in that URL path it lets the Django project know what to do with that URL.
The main file we're gonna be using here is this settings.py file and this is kind of like the command center, the main configuration to your Django app. You see here we have the database setup here and any time we are installing or changing up our database the main configuration all happens here. So when you want Django to know about a new install we'll usually add it into this list here so there's a lot that's done here and that's all we need to know for now.
Running the development server and default pages
Okay, so now that I covered the file structure I want to show you how to launch a basic Django app and show you what actually happens when we turn on our server. So that manage.py file we're gonna use that right now, and in your command prompt in your root directory of your Django project make sure you're in there, go ahead and run python manage.py runserver and go ahead and hit enter and give it a second.
Once you see this it means your server is on and you can now see your project on port 8000 right here. So something that I do is actually save that port number as a saved tab here so I don't have to keep typing this same... go ahead and manually type it in and save it just for this convenience here. So on port 8000 our server is running and you're gonna see it respond to what we're doing here. This is the basic setup that Django gives us and we have access to two different pages right now. This is the base template and if you look in our URL path we also have access to this admin page here. I won't go into it but I'll just show you really quickly what it is, so /admin and looks like we don't have our database set up yet but that should set up, that should go to our admin page. It kind of just gives us an idea of what's actually happening here.
We can turn off our server by clicking Ctrl+C and sometimes you need to double click that and that turns off our server and again we can turn it back on by doing runserver and activating it.
Apps concept and when to use them
Now that I covered the basic file structures of Django we need to talk about the concept of apps within our Django project. This is a little confusing for beginners sometimes; it was for me. But you really see the idea of app scale when your project becomes more than a few pages. The example I like to think of is the concept of Facebook and how they would structure their projects. So Facebook is your base project—that's the Django application we just set up—and Facebook is comprised of multiple sections. They have things like profile pages, they have things like the newsfeed and groups, and these in this example would be different apps within the entire Facebook project.
A good rule of thumb to think about apps is if you can't explain what that file does in one sentence, it needs to be its own app. This is where we can separate our database files so we're not coming up with massive file sizes and creating a bunch of bugs later. We're able to section off our code and keep things a lot simpler. For the sake of this project I'm gonna only create one app and I think that's gonna be accounts. I'm gonna keep it simple because our project is too small to need to scale that way, but I will be addressing it later and you'll be able to get the concept from this project here. If you're a beginner and you don't understand the concept of multiple apps I would recommend just sticking to one until you actually see why it makes sense. If you don't know why it helps, I recommend not using it and you'll know once a project is so big and it'll just make sense to you to say, 'Ok, well yeah, the file structure is getting too big; this needs to be sectioned out.' You shouldn't face that issue until your project actually grows, so for a beginner I would recommend leaving it alone until it makes sense and you read up more about it because you might just confuse your project a little, you know, more than it needs to be.
Creating an app and registering it
Now that I've covered apps let's go ahead and actually make one in our project. So I'll go ahead and turn off my server and I'll move this file here so we can actually see what's happening. To create our app it's kind of similar to our startproject command except we're going to use python manage.py and we're gonna say startapp and we need to give our app a name just like we gave our project. So I'll call our app accounts and go ahead and hit enter and you'll see this folder appear here and it just gives us again another set of boilerplate files that we can start working within them.
I'll just go ahead and run down that structure briefly—just like I did with the initial project. Okay, so here's our accounts folder and the main files we need to focus on here are gonna be the admin panel files and we'll cover this once we start setting up the admin panel. The models file—this is going to be where we actually build our database in class-based objects and it'll represent the file structure that we're gonna have in our database. And this views.py file—these are basically going to be the functions or classes that our URL patterns go to and trigger to actually render out a template and so on. There's gonna be a lot done from here and that covers that.
One thing we'll need to do is configure our app and let our project know, 'Hey, be aware of this app; we're now connected.' In the settings.py, in INSTALLED_APPS, let's go ahead and add it in as a list item and you just need to give it the name of whatever you named your app, and ours is accounts. Once we have that Django now knows that we have this folder within it. In the next video we're gonna work on your routing and views and actually render some responses when we have our server on and actually type in a URL path.