Running the Server

python manage.py runserver 0.0.0.0:8000 will run the server for Workspaces. On your own computer, you probably don't need the 0.0.0.0:8000 part.

python manage.py migrate will apply all pending migrations from all apps. More on apps later.

What are migrations?

Migrations are a way of moving a database from one design, a specific set of tables and columns, to a new one. Migrations are reversible, too. The fact that they can be done backwards and forwards is what gives them their name.

0:00

[MUSIC]

0:05

>> Okay, so you have Django installed,
and you've created a project.

0:08

You're well on your way
to becoming a Djangonaut.

0:10

Yeah, that's the collective noun for
people that use Django.

0:13

Okay, enough about silly nouns, let's
go see how to get Django server up and

0:16

running so we can get to the fun
part of building an app.

0:19

So, remember the manage.py file?

0:21

The one that I said would be our main
control script, our entry point for

0:26

our project?

0:27

Well, we're going to use it.

0:29

If you did the Flask courses,
you'll remember that we had to tell Flask

0:32

to listen for
requests on 0.0.0 in port 8000.

0:36

Well we have to do that with Django too.

0:38

If you didn't take the Flask
courses we have to do this so

0:41

that Django can talk through our work
spaces server to the actual internet so

0:46

that we can see it from wherever
we are are on the internet.

0:50

Let's go ahead and run this.

0:51

So what we're gonna do is we're gonna do,
first we gotta get in there.

0:53

cd learning_site/.

0:56

I'm in the same directory as manage.py.

1:00

So, I'm gonna do python manage.py and
then I'm gonna do run server.

1:06

And the address I'm gonna
go to is 0.0.0.0:8000.

1:14

And I hit Return, and you're gonna see
a bunch of stuff run through here.

1:18

Don't worry about this too much right now,
we'll get back to it in a minute.

1:21

And then I'm gonna come up here to the
Preview icon, and I'm gonna click 8000.

1:25

Check that out.

1:27

I've got a Django powered page
telling me everything is good.

1:32

That's awesome, wonderful,
glad to have that.

1:35

So let's go back.

1:37

And if we look here in our output we have
this, you have unapplied migrations.

1:42

What is that?

1:43

What is that about?

1:44

That's really weird.

1:45

So, what this is,
is Django comes with some migrations.

1:52

So migrations are a way of moving your
database from one design to another.

1:57

You're moving, you're changing
the structure, the organization or

2:00

the data in the database from
one configuration to another.

2:05

Often you'll find that your
table now needs a new column

2:08

to handle an extra little bit of data.

2:10

Or maybe you want to store a preferred
name alongside the legal name that you've

2:13

already collected.

2:14

You'd add this column with a migration.

2:17

Which moves the table from
one design into the other.

2:20

You can reverse this process too,
and remove the column.

2:23

Since you're moving back and forth,
that's what gives it the name migration.

2:28

So, we're gonna kill our server here.

2:30

I can do that with Ctrl+C.

2:33

And we're gonna run our migrations.

2:36

So.

2:37

We do python manage.py migrate.

2:40

Just like it says to do right here.

2:43

We follow the instructions
that run server gives us.

2:46

It prints out a whole bunch of stuff.

2:48

And we can see that it found
a few different operations and

2:53

it's running migrations for
I think four different apps.

2:57

So, migrations, I said,
were about the database.

3:00

We didn't set up a database.

3:02

Well, Django comes with one.

3:05

So, let's refresh.

3:08

And if we look here,
then we have this db.sqlite3.

3:12

Django automatically
creates this database for

3:14

us if we don't tell it
something else to do.

3:17

So we have this automatically.

3:18

Now this is SQLite.

3:19

This is fine for testing your idea or
playing around in.

3:24

If you get really serious, you're
gonna wanna move to a real database,

3:26

you're wanna move to MySQL or
Postgres, and

3:29

you definitely wanna use one of those two,
or one of the other wonderful databases

3:33

that are out there before you go
live on the internet and have users.

3:39

Let's try doing our run server again.

3:42

Push Up a couple of times.

3:43

And this time we don't
get any error messages.

3:46

And if we come over here and

3:47

refresh our site,
it still loads just like it's supposed to.

3:52

If you didn't quite get what
migrations are about, don't worry.

3:55

We're gonna be creating and
running more of them later on.

3:57

But, the first thing you have to do
with any new technology is make your own