Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Django REST framework and Django Channels play nicely

I want to make the Django REST Framework work with web sockets and came across Django Channels. Can I build Channels on top of the REST Framework?

I read here that it is WIP, however I was wondering if there is an easy 'light' way to tie the two together. Any advice is appreciated, I'm still rather new to Django.

like image 399
Roy van der Valk Avatar asked Jul 14 '16 12:07

Roy van der Valk


People also ask

Why use Django REST framework instead of Django?

Django REST framework (DRF) is a powerful and flexible toolkit for building Web APIs. Its main benefit is that it makes serialization much easier. Django REST framework is based on Django's class-based views, so it's an excellent option if you're familiar with Django.

Should I use Django or Django REST?

In order to complete your app, if you want to code more python / Django code then use Django, to the contrary if you want to code more JavaScript code then use Django restful API. For web application itself I like using Django ,because it is based on python ,it is much cleaner and shorter than js.

Is Django different from Django REST framework?

Django is the web development framework in python whereas the Django Rest Framework is the library used in Django to build Rest APIs. Django Rest Framework is especially designed to make the CRUD operations easier to design in Django.


1 Answers

Roy van der Valk - have you had any luck so far?

I've had success launching our platform which uses traditional Django views, Django-Rest-Framework views and channels 'consumers'.

There is a guncorn/wsgi substitute called Daphne - it will mitigate the request itself (wherever http/websocket etc..) to it's correct response handler. It uses a separate request handler called - asgi.

The trick was in crafting the django application level urls to dispatch the 'http.request' channel to the correct urls configuration which handled our traditional Django and Django-Rest-Framework views.

The rest of the asgi channels had 'views' or channels created to handle them appropriately.

Let me know if this is enough to get you moving in the right direction. I can provide you with links/details if you provide me with specific problems.

like image 77
Richael Avatar answered Oct 04 '22 13:10

Richael