Today, we're thrilled to host Jacob Kaplan-Moss.
Jacob's a former Herokai and long-time core contributor to Django, and he's here to share an in-depth look at something that he believes will define the future of the framework.
Although these APIs aren’t part of Django yet, they will be soon!There are a bunch of rooms, and everyone in the same room can chat, in real-time, with each other (using Web Sockets). For the full messy detailed code, see the project on Github.) And then a view (chat/views.py, and the associated and chat/template): def chat_room(request, label): # If the room with the given label doesn't exist, automatically create it # upon first visit (a la etherpad).You can visit my deployment of the example online, check out the code on Git Hub, or deploy your own copy with this button (which requires a free Heroku account, so sign up for that first): Note: you'll need to scale the dynos! I’ll walk through the steps to take to build this app, and highlight the import bits and concepts along the way. Foreign Key(Room, related_name='messages') handle = models. room, created = Room.objects.get_or_create(label=label) # We want to show the last 50 messages, ordered most-recent-last messages = reversed(room.messages.order_by('-timestamp')[:50]) return render(request, "chat/room.html", ) At this point, we have a functioning — if unexciting — Django app.Finally, we need to wire up a callback to fire when new data is received on the Web Socket: Simple stuff: just append a row to our transcript table, pulling data out of the received message.If I run this code now, it won’t work — there’s nothing listening to Web Socket connections, just HTTP. To “channel-ify” this app, we’ll need to do three things: install Channels, set up a setting.