Introduction to the Django Admin Page
Hey there, how's it going everybody? In this video, we'll be learning how to access the admin page of our Django site. Now, the admin page is a great way to see what data is on your site. It also gives you a nice GUI for creating, updating, or deleting that data. Now we saw this admin login page earlier in the series, but we didn't do anything with it.
So just to remind you what it looked like, let's start up our development server, which I already have running from the command line. And now let's open up our page. So I've got the home page here. Okay, so within our home page, let's navigate to /admin like we saw earlier, and we can see that we get this admin login. Now we can't log in here yet because there are no default usernames or passwords or anything like that. We actually have to create an admin user so that we can log in here for the first time.
Running Initial Database Migrations
So let's bring back up our command line, and I will stop the development server. And now let me clear the screen here so that we have some room. And now the command to create a new user for a superuser to log in to that admin page, we can say python manage.py and this is createsuperuser all one command. Now, if we were to run this right now, then it's not going to work. So let me run this and show you why it isn't going to work right now. So I ran that, and we got a lot of error feedback here, but the very last line of this error gives us a good hint. It says operational error: no such table: auth_user.
So the problem here is that we haven't created the database that we'll be using for this project yet, but that's very easy to do. All we have to do is run a few migration commands. Now, database migration is something that we'll talk about more throughout the series, but basically, it allows us to apply changes to a database. Now, since we haven't created a database yet, this first migration will create the database and add a bunch of default tables for us, and this auth table here is one of those tables that is going to get created. And then it will allow us to run that createsuperuser command.
So let's go ahead and run these migrations. So again, let me clear the screen. So the first thing we can do here is to say python manage.py makemigrations. So let's run that. And when we run that, it tells us that there were no changes detected. And now, if we had created some of our own database tables or models, then we would have seen some changes there, but we'll see that in the next video when we actually work with the database. So makemigrations just detects the changes and prepares Django to update the database, but it doesn't actually run those changes yet. In order to apply the migrations, we need to say python manage.py and then migrate.
Creating a Superuser and Logging In
So if we run that, then we can see that it ran through some migrations, and those all came back as okay. So that auth_user table should now exist. So now if I rerun that command that we tried earlier—so let me clear the screen and hit up a couple of times to find that createsuperuser command—if we rerun that command now, now it seems to be working. And it's asking us for a username. So I'm going to enter a username of CoreyMS and an email address, I'll just put in [email protected]. And now for a password, I'm just going to do testing321 and again, testing321. Now obviously, you'll want to make your password stronger in a real-world application. Okay, so now we can see that that superuser was created successfully.
So now let's run our server again and reload that admin page in our browser. So we'll do python manage.py runserver. And once that is running, we can go back to our browser here and reload the login page for the admin. And now we should be able to sign in with that username and password that we used to create the superuser. So for me, that was CoreyMS, and I used a password of testing321. So let's try to log in with that. And we can see that now we logged in successfully.
Navigating the Admin Dashboard
Okay, so this is the default admin page for the Django site. So let me make this a little larger here so that you can see everything. Now, this is one great thing about Django is that it comes with this really awesome admin site by default. So this allows us to do a lot to work on the backend that would take a ton of effort to implement if we were to do this from scratch on our own. So let's take a quick look around. So we can see that we have groups and users here.
Now if I click on groups, then it says that we have zero groups right now. So we could add some if we want, but I'm not going to right now. Now if we look towards the top, then we can see that we have some breadcrumbs here that show us exactly where we are on the admin page. So right now we are within Authentication and Authorization and within groups. So to go back a level, I can just click up one level here, and now we're back to the page that we were at before.
So now let's click on users and we have one user here right now, and it's the one that we just created. So if I click on that user, then it takes us to a page with a lot more details where we can add more stuff or change some things that we've already added. So we can see we have our username here of CoreyMS. Now for our password, we set our password to testing321, but now we have this big long hashed password. So it never even stores our raw password. So Django handled the hashing of that password for us and does all of the proper checks when we log in in order to see if our entered password gets hashed to that correct value. So that's a lot of added security there that we just get for free, so that's awesome.
So moving down here, we can see that we have a first name, last name, we can change our email address, we can change our permissions and all kinds of stuff, but I'm not going to change anything in here right now. I just wanted you all to see what all was possible. So let's go back to the top and click on the breadcrumb that takes us back to our list of users.
Adding and Managing Users in the Admin
And now on the top right here, let's click on Add User. So now we can actually create a new user. So I'm going to create a new user here. So I'll do testuser and for the password, I'll do just the same thing, testing321, and we need to confirm this user's password. And now let's click on Save and continue editing. And we could continue adding fields for this user here if we wanted to. So for example, if we wanted to add an email address, I could just say [email protected].
Now, let me scroll down here to permissions so that we can see what permissions it gave this new user. So we can see that it created this as an active user, but it didn't give this person staff status by default and it also didn't give them superuser status by default. Now, staff status means that this person would be able to log into the admin site and superuser status means that they would have all of the permissions, so, you know, to delete other users and things like that.
Now if we scroll all the way to the bottom here, then we can see that we can also delete this new user, but I'm going to go ahead and leave him here. So let's go ahead and save that email change that we made and then go back to our main page.
Recent Actions & Conclusion
Now one last thing I'd like to point out over here to the right is that it also shows us our latest changes. So we can see here that we have a plus sign next to the user, which means that we created that user. So that's a great way to keep track of what's going on. And we can see that we have a pencil icon here next to the test user, and that is where we edited the user and added their email address.
Okay, so I think that is going to do it for this video. So in this video, we just got a quick look at the admin page for Django, but we're going to be using this page a lot throughout the series. Now, in the next video, we'll be learning how to work with databases so that we can get started with creating some real data for our website instead of using the dummy data that we have for now. And we're also going to learn how to get that data added to this admin page so that we can come in here and create, update, and delete posts right from here within the admin page.
But if anyone has 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 enjoyed these tutorials and would like to support them, 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.