G
G
gromyko212020-09-08 14:33:34
Django
gromyko21, 2020-09-08 14:33:34

How to create rooms on django channels?

Tell me how to add rooms to django channels? I'm sitting in a stupor, I read the documentation several times. As I understand it, you need to change something in cunsomer

def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name,
            self.channel_name
        )
        self.accept()

Most likely at this moment. You need this to connect users with a private chat.
Just in case, I'll throw a model.
class Message(models.Model):
    '''
    Обмен личными сообщениями
    '''
    author = models.ForeignKey(User, related_name='author_messages', on_delete=models.CASCADE)
    content = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)

    def last_10_messages():
        return Message.objects.order_by('-timestamp').all()[:10]

I reviewed a bunch of articles, guides, documentation on this subject, but unfortunately I could not figure it out myself.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-09-08
@tumbler

And try to start by writing a chat the old fashioned way, through forms and plain HTML. When you get your hands on it, add an API, if this is not enough, go to channels.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question