File: part_1.rst

package info (click to toggle)
python-django-channels 4.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,036 kB
  • sloc: python: 3,109; makefile: 155; javascript: 60; sh: 8
file content (386 lines) | stat: -rw-r--r-- 11,971 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
Tutorial Part 1: Basic Setup
============================

.. note::
    If you encounter any issue during your coding session, please see the :doc:`/topics/troubleshooting` section.


In this tutorial we will build a simple chat server. It will have two pages:

* An index view that lets you type the name of a chat room to join.
* A room view that lets you see messages posted in a particular chat room.

The room view will use a WebSocket to communicate with the Django server and
listen for any messages that are posted.

We assume that you are familiar with basic concepts for building a Django site.
If not we recommend you complete `the Django tutorial`_ first and then come
back to this tutorial.

We assume that you have `Django installed`_ already. You can tell Django is
installed and which version by running the following command in a shell prompt
(indicated by the ``$`` prefix):

.. code-block:: sh

    $ python3 -m django --version

We also assume that you have :doc:`Channels and Daphne installed
</installation>` already. You can check by running the following command:

.. code-block:: sh

    $ python3 -c 'import channels; import daphne; print(channels.__version__, daphne.__version__)'

This tutorial is written for Channels 4.0, which supports Python 3.7+ and Django
3.2+. If the Channels version does not match, you can refer to the tutorial for
your version of Channels by using the version switcher at the bottom right corner
of this page, or update Channels to the newest version.

This tutorial also **uses Docker** to install and run Redis. We use Redis as the
backing store for the channel layer, which is an optional component of the
Channels library that we use in the tutorial. `Install Docker`_ from its
official website - there are official runtimes for Mac OS and Windows that
make it easy to use, and packages for many Linux distributions where it can
run natively.

.. note::
    While you can run the standard Django ``runserver`` without the need
    for Docker, the channels features we'll be using in later parts of the
    tutorial will need Redis to run, and we recommend Docker as the easiest
    way to do this.

.. _the Django tutorial: https://docs.djangoproject.com/en/stable/intro/tutorial01/
.. _Django installed: https://docs.djangoproject.com/en/stable/intro/install/

.. _Install Docker: https://www.docker.com/get-docker

Creating a project
------------------

If you don't already have a Django project, you will need to create one.

From the command line, ``cd`` into a directory where you'd like to store your
code, then run the following command:

.. code-block:: sh

    $ django-admin startproject mysite

This will create a ``mysite`` directory in your current directory with the
following contents:

.. code-block:: text

    mysite/
        manage.py
        mysite/
            __init__.py
            asgi.py
            settings.py
            urls.py
            wsgi.py

Creating the Chat app
---------------------

We will put the code for the chat server in its own app.

Make sure you're in the same directory as ``manage.py`` and type this command:

.. code-block:: sh

    $ python3 manage.py startapp chat

That'll create a directory ``chat``, which is laid out like this:

.. code-block:: text

    chat/
        __init__.py
        admin.py
        apps.py
        migrations/
            __init__.py
        models.py
        tests.py
        views.py

For the purposes of this tutorial, we will only be working with ``chat/views.py``
and ``chat/__init__.py``. So remove all other files from the ``chat`` directory.

After removing unnecessary files, the ``chat`` directory should look like:

.. code-block:: text

    chat/
        __init__.py
        views.py

We need to tell our project that the ``chat`` app is installed. Edit the
``mysite/settings.py`` file and add ``'chat'`` to the **INSTALLED_APPS** setting.
It'll look like this:

.. code-block:: python

    # mysite/settings.py
    INSTALLED_APPS = [
        'chat',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]

Add the index view
------------------

We will now create the first view, an index view that lets you type the name of
a chat room to join.

Create a ``templates`` directory in your ``chat`` directory. Within the
``templates`` directory you have just created, create another directory called
``chat``, and within that create a file called ``index.html`` to hold the
template for the index view.

Your chat directory should now look like:

.. code-block:: text

    chat/
        __init__.py
        templates/
            chat/
                index.html
        views.py

Put the following code in ``chat/templates/chat/index.html``:

.. code-block:: html

    <!-- chat/templates/chat/index.html -->
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8"/>
        <title>Chat Rooms</title>
    </head>
    <body>
        What chat room would you like to enter?<br>
        <input id="room-name-input" type="text" size="100"><br>
        <input id="room-name-submit" type="button" value="Enter">

        <script>
            document.querySelector('#room-name-input').focus();
            document.querySelector('#room-name-input').onkeyup = function(e) {
                if (e.key === 'Enter') {  // enter, return
                    document.querySelector('#room-name-submit').click();
                }
            };

            document.querySelector('#room-name-submit').onclick = function(e) {
                var roomName = document.querySelector('#room-name-input').value;
                window.location.pathname = '/chat/' + roomName + '/';
            };
        </script>
    </body>
    </html>

Create the view function for the room view.
Put the following code in ``chat/views.py``:

.. code-block:: python

    # chat/views.py
    from django.shortcuts import render


    def index(request):
        return render(request, "chat/index.html")

To call the view, we need to map it to a URL - and for this we need a URLconf.

To create a URLconf in the chat directory, create a file called ``urls.py``.
Your app directory should now look like:

.. code-block:: text

    chat/
        __init__.py
        templates/
            chat/
                index.html
        urls.py
        views.py

In the ``chat/urls.py`` file include the following code:

.. code-block:: python

    # chat/urls.py
    from django.urls import path

    from . import views


    urlpatterns = [
        path("", views.index, name="index"),
    ]

The next step is to point the root URLconf at the **chat.urls** module.
In ``mysite/urls.py``, add an import for **django.urls.include** and
insert an **include()** in the **urlpatterns** list, so you have:

.. code-block:: python

    # mysite/urls.py
    from django.contrib import admin
    from django.urls import include, path

    urlpatterns = [
        path("chat/", include("chat.urls")),
        path("admin/", admin.site.urls),
    ]

Let's verify that the index view works. Run the following command:

.. code-block:: sh

    $ python3 manage.py runserver

You'll see the following output on the command line:

.. code-block:: text

    Watching for file changes with StatReloader
    Performing system checks...

    System check identified no issues (0 silenced).

    You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
    Run 'python manage.py migrate' to apply them.
    August 19, 2022 - 10:05:13
    Django version 4.1, using settings 'mysite.settings'
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CONTROL-C.

.. note::
    Ignore the warning about unapplied database migrations.
    We won't be using a database in this tutorial.

Go to http://127.0.0.1:8000/chat/ in your browser and you should see the text
"What chat room would you like to enter?" along with a text input to provide a
room name.

Type in "lobby" as the room name and press enter. You should be redirected to
the room view at http://127.0.0.1:8000/chat/lobby/ but we haven't written the
room view yet, so you'll get a "Page not found" error page.

Go to the terminal where you ran the ``runserver`` command and press Control-C
to stop the server.

Integrate the Channels library
------------------------------

So far we've just created a regular Django app; we haven't used the Channels
library at all. Now it's time to integrate Channels.

Let's start by creating a routing configuration for Channels. A Channels
:doc:`routing configuration </topics/routing>` is an ASGI application that is
similar to a Django URLconf, in that it tells Channels what code to run when an
HTTP request is received by the Channels server.

Start by adjusting the ``mysite/asgi.py`` file to include the following code:

.. code-block:: python

    # mysite/asgi.py
    import os

    from channels.routing import ProtocolTypeRouter
    from django.core.asgi import get_asgi_application

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

    application = ProtocolTypeRouter(
        {
            "http": get_asgi_application(),
            # Just HTTP for now. (We can add other protocols later.)
        }
    )

Now add the Daphne library to the list of installed apps, in order to enable
an ASGI versions of the ``runserver`` command.

Edit the ``mysite/settings.py`` file and add ``'daphne'`` to the top of the
``INSTALLED_APPS`` setting. It'll look like this:

.. code-block:: python

    # mysite/settings.py
    INSTALLED_APPS = [
        'daphne',
        'chat',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]

You'll also need to point Daphne at the root routing configuration.
Edit the ``mysite/settings.py`` file again and add the following to the bottom
of it:

.. code-block:: python

    # mysite/settings.py
    # Daphne
    ASGI_APPLICATION = "mysite.asgi.application"

With Daphne now in the installed apps, it will take control of the
``runserver`` command, replacing the standard Django development server with
the ASGI compatible version.

.. note::
    The Daphne development server will conflict with any other third-party
    apps that require an overloaded or replacement runserver command.
    In order to solve such issues, make sure ``daphne`` is at the top of your
    ``INSTALLED_APPS``, or remove the offending app altogether.

Let's ensure that the Channels development server is working correctly.
Run the following command:

.. code-block:: sh

    $ python3 manage.py runserver

You'll see the following output on the command line:

.. code-block:: text

    Watching for file changes with StatReloader
    Performing system checks...

    System check identified no issues (0 silenced).

    You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
    Run 'python manage.py migrate' to apply them.
    August 19, 2022 - 10:20:28
    Django version 4.1, using settings 'mysite.settings'
    Starting ASGI/Daphne version 3.0.2 development server at http://127.0.0.1:8000/
    Quit the server with CONTROL-C.

Notice the line beginning with ``Starting ASGI/Daphne …``. This indicates that the
Daphne development server has taken over from the Django development server.

Go to http://127.0.0.1:8000/chat/ in your browser and you should still see the
index page that we created before.

Go to the terminal where you ran the ``runserver`` command and press Control-C
to stop the server.

This tutorial continues in :doc:`Tutorial 2 </tutorial/part_2>`.