django / channels

Developer-friendly asynchrony for Django

Home Page:https://channels.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

There is something wrong with tutorial part 2.

xiezhuojin opened this issue · comments

I follow the tutorial in part 2. But there is something wrong with this code.

application = ProtocolTypeRouter({
  "http": get_asgi_application(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            chat.routing.websocket_urlpatterns
        )
    ),
})

And the error message is

WebSocket HANDSHAKING /ws/chat/lobby/ [127.0.0.1:63654]
Exception inside application: AuthMiddleware cannot find session in scope. SessionMiddleware must be above it.
Traceback (most recent call last):
  File "c:\Users\itstudy\Desktop\work\learning\python\django\.venv\lib\site-packages\channels\staticfiles.py", line 44, in __call__
    return await self.application(scope, receive, send)
  File "c:\Users\itstudy\Desktop\work\learning\python\django\.venv\lib\site-packages\channels\routing.py", line 71, in __call__    
    return await application(scope, receive, send)
  File "c:\Users\itstudy\Desktop\work\learning\python\django\.venv\lib\site-packages\channels\auth.py", line 181, in __call__      
    self.populate_scope(scope)
  File "c:\Users\itstudy\Desktop\work\learning\python\django\.venv\lib\site-packages\channels\auth.py", line 167, in populate_scope
    raise ValueError(
ValueError: AuthMiddleware cannot find session in scope. SessionMiddleware must be above it.

After that I added CookieMiddleware and SessionMiddleware before AuthMiddleware and the error goes away. Is the tutorial missing something like this?

My environment
OS: windows10
python: 3.10
django: 3.2.8
channels: 3.0.4

Are you sure you've used AuthMiddlewareStack and not just AuthMiddleware?

You are right! It's a typo! Thanks for your mentioning.