Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that we have a working understanding of how web requests traditionally worked, and how websockets changes that behavior, let’s take a look at what we’re going to build to make use of Django, Channels, and websockets
You can read the Channels documentation. If you'd like a Git repository for the project, here you go!
Over the course of this workshop,
0:00
we're going to be learning about
channels by adding a real time dashboard
0:01
to a Django project.
0:04
This is the kind of addition
that channels makes possible and
0:05
which can allow you to start super
charging your Django products.
0:08
Let's take a quick tour of what we'll be
doing by looking at our finished project.
0:11
So we've started with
an existing Django project,
0:16
we're tracking how members of city
council voted on each measure.
0:18
It's a pretty simple app since
the focus of the workshop is channels,
0:22
not Django itself.
0:25
Due to this will just add
a modifier data through the admin
0:27
set of building any sort of
crowd features in the app.
0:30
Let's look at what we have available here.
0:33
Now a project has one app the votes app
and the votes app has two models votes and
0:35
voters, or rather vote and voter,
because the admin pluralizes this.
0:40
So we go look at the voter page we
can look at say Mayor Adamson, and
0:46
we see that we have a name which
is the person's last name,
0:50
and we have a title which would be
their position on city council.
0:53
And now that we have some voters,
we can also look at the votes.
0:56
So let's actually add a new vote.
1:02
So at the top of the vote
we list the issue.
1:05
So this would be like a proposal number or
pave Lombard Street or whatever.
1:07
And then we have which
category it belongs to and
1:13
you can add more of these in
the model's .py if you need to, and
1:16
then you can choose who voted yes and
who voted no for that particular vote.
1:20
And if we look at our finished dashboard
here, we'll see that there is a listing of
1:24
each council member and
how they voted on particular categories.
1:28
If we open up the admin
interface in another window,
1:31
which we conveniently already have open,
and we add a new issue.
1:34
So let's say this is issue number 89 and
it is for Housing.
1:37
And Mayor Adamson and Tompkins, and
1:43
Perez, all voted yes, but Bello voted no.
1:47
And then let's save this one.
1:51
And we can see that the vote
counts updated in real time.
1:54
Now, behind the scenes, our browser in the
window where we've loaded the dashboard,
1:58
has opened a web connection to our
Django project running with channels.
2:02
When we save a vote, we're firing
an event that channels picks up and
2:06
routes a special event handling code
to that channels calls a consumer.
2:09
The consumer gets the message and
send some data over the website,
2:13
gets the connection back to the browser.
2:16
The JavaScript in the browser
then updates the dashboard.
2:18
If any of this seems confusing or
like magic now,
2:21
it should get clearer as
we dive into the code.
2:24
In the next video will start getting
our hands dirty by installing and
2:26
configuring the channels package.
2:29
You need to sign up for Treehouse in order to download course files.
Sign up