Search code examples
djangodjango-channels

Get the current URL on connection in Django Channels


consumers.py

async def connect(self):
    pk = self.scope["user"].pk
    self.room_group_name = await self.get_room(pk)
    print(self.room_group_name)

    # Join room group
    await self.channel_layer.group_add(
        self.room_group_name,
        self.channel_name
    )

    await self.accept()

routing.py

from django.urls import path
from . import consumers

websocket_urlpatterns = [
    path('ws/chat/<int:pk>/', consumers.ChatConsumer.as_asgi()),
]

How to get the current url on connection

Any help is highly appreciated

Thank you.


Solution

  • self.scope["url_route"]["kwargs"]["pk"] this should do...