Locked lesson.
About this lesson
Databases involve a two-step process: migration, and pushing the migration to a database. In this lesson, we'll cover both steps of this process for your new project.
Exercise files
Download this lesson’s related exercise files.
Migrate Database For Admin Area And Create Superuser.docx57.3 KB Migrate Database For Admin Area And Create Superuser - Solution.docx
55.7 KB
Quick reference
Migrate Database For Admin Area And Create Superuser
In this video we'll learn how to Migrate the Django Database and Create a Superuser for the Django Admin area.
When to use
Whenever you create a new project, you always want to migrate the server right away, and create a super user for the Django admin area.
Instructions
To migrate a Django database, use the command:
python manage.py migrate
To create a super user, use the command:
winpty python manage.py createsuperuser
To view the Django Admin area, navigate your web browser to:
http://localhost:8000/admin
Hints & tips
- python manage.py migrate
- winpty python manage.py createsuperuser
- http://localhost:8000/admin
- 00:04 Okay, in this video I want to talk about migrations of the database.
- 00:08 Now if we click back to our original Git Bash terminal that has the server running,
- 00:13 you notice when I broke out of the server in the last video,
- 00:16 we got this message you have 17 unapplied migrations.
- 00:20 So what is that all about?
- 00:21 Well, Django out of the box comes with an administration area on the back end that
- 00:26 allows you to do database stuff.
- 00:28 But anytime you're dealing with databases, it's always a two step process.
- 00:32 You make a migration and then you push the migration into the database.
- 00:36 And now that doesn't mean a lot to you and it really doesn't need to at this point.
- 00:40 The point is right out of the box when you install Django.
- 00:43 There are some migrations for the database for
- 00:46 that back end administration area that need to be migrated.
- 00:49 So let's do that really quickly in this video and
- 00:52 then I'll show you the administration area.
- 00:55 So I'm going to hit Ctrl+C to break out of this server.
- 00:58 And to push these migrations,
- 01:01 we just type in pythonmanage.py.
- 01:05 And then just the command is migrate.
- 01:09 And when we do that, it applies those 17 migrations.
- 01:13 And everything's good to go.
- 01:15 So we can clear the screen.
- 01:16 So strictly speaking, that's all you have to do.
- 01:19 And in fact, if we run our web server again and
- 01:25 go back to our app, and
- 01:27 then navigate to localhost:8000/admin.
- 01:33 We'll see this administration area that we can log into.
- 01:36 Now, we don't have a username or a password.
- 01:39 So we can't get in there yet, but we can create one very simply.
- 01:42 So let's head back over to our terminal and I'm using the second Git Bash terminal
- 01:47 so we can keep the web server running in the first one.
- 01:50 So what we want to do is create a user or what Django calls a super user.
- 01:54 So to do that, we just type in winpty python
- 02:00 manage.py and then createsuperuser.
- 02:05 And before I hit Enter, why are we doing this winpty?
- 02:08 I said usually when we run commands we type in python manage.pi and
- 02:13 then the command this time we're doing winpty.
- 02:16 Well, that's strictly because we're using the Git Bash terminal.
- 02:20 If you're using any other type of terminal for this specific comand,
- 02:23 you won't have to do that winpty.
- 02:25 But since we're using to get bashed terminal for this specific man,
- 02:29 we have to type winpty first, and then the command.
- 02:32 So, but hit Enter.
- 02:33 And it's going to ask you to pick a username, and
- 02:36 you can pick any username you want.
- 02:37 I'm going to just pick admin.
- 02:40 And now it asks for your email address.
- 02:42 You can type this in if you want, but you don't have to.
- 02:45 Now it's asking for a password.
- 02:47 So I'm going to type in just a random password.
- 02:52 Now you'll notice as I'm typing, nothing appears on the screen.
- 02:55 That's normal.
- 02:56 That's a security process, just to make sure that,
- 02:58 I don't know, it doesn't get stolen.
- 03:01 It's going to ask you to type it again.
- 03:02 So go ahead and type it again.
- 03:04 And your super user was created successfully.
- 03:06 So now we can head back over to this localhost;8000/admin.
- 03:13 And we can type in that username we just created and then go ahead and
- 03:17 type that password and then click Login.
- 03:20 And when you do you see the Django administration area?
- 03:24 And like I said earlier,
- 03:25 this is just a way that you can access the database that comes with Django.
- 03:29 In our course here, we're not really going to do any database work.
- 03:32 That's more of an advanced topic.
- 03:34 But if you did want to do some database stuff, you can see it visually,
- 03:38 right here.
- 03:39 So we can click on users, we see here's that admin user we just created.
- 03:43 There's my email address, and we can click on this and do all kinds of things.
- 03:47 We can add first name, last name, if we want, we can change it right here.
- 03:52 If we want to change the password, you can come down here right here and
- 03:57 click this and then type in your new password and stuff like that.
- 04:02 And that's pretty cool.
- 04:03 If you want to delete this user, you can delete it right there.
- 04:06 And very neat.
- 04:07 So this isn't all that interesting because we're not actually doing any database
- 04:12 stuff, but I just wanted to kind of show it to you really quickly.
- 04:16 And because you really should migrate the server every time you create a new
- 04:19 Django project, just to get those migrations out of the way,
- 04:22 you don't necessarily have to do that.
- 04:24 But it's best practices to do it, and the reason why is so that
- 04:28 you can use this Django administration area later on if you want it to.
- 04:31 And like I said,
- 04:32 we're not going to use this Django administration tool in this course at all.
- 04:36 But I just wanted to show it to you so you're aware of it, okay.
- 04:39 So that's all for this video and the next video,
- 04:43 we'll start to build out our new app.
Lesson notes are only available for subscribers.