channels==2.1.2
| channels-redis==2.2.1
| daphne==2.2.0
| Django==1.11.6
我已专门升级到Channels 2,以便能够从使用者内部访问和修改会话(并在视图中访问它),但这似乎不起作用。基本上,我想识别AnonymousUser
并向他们发送消息(每个消息都是他自己的,而不是全部在一起)。
这是我的routing.py
文件:
application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
URLRouter(
core.routing.websocket_urlpatterns
)
),
})
这是我的consumers.py
文件:
class ChatConsumer(WebsocketConsumer):
def connect(self):
if self.scope['user'].is_authenticated:
user_id = str(self.scope['user'].id)
self.scope['session']['user_identifier']= user_id
self.group_name = user_id
else:
user_id = str(self.scope['user']) + str(uuid.uuid4())
self.scope['session']['user_identifier'] = user_id
self.group_name = user_id
self.scope['session'].save()
print(f" in consumer: {self.scope['session']['user_identifier']}")
# Join room group
async_to_sync(self.channel_layer.group_add)(
self.group_name,
self.channel_name
)
self.accept()
在需要使用send
的视图中,如果有AnonymousUser,我正在尝试从会话中获取group_name(user_identifier
):
def get_spotify_link(request):
if request.user.is_authenticated:
user_identifier = str(request.user.id)
else:
user_identifier = request.session['user_identifier']
print(f"in get_spotify_link: {user_identifier}")
但是,我间歇性地(有99%的时间)得到KeyError: 'user_identifier'
。
Internal Server Error: /get_spotify_link/
Traceback (most recent call last):
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/myusername/PycharmProjects/artist_notify/core/views.py", line 442, in get_spotify_link
user_identifier = request.session['user_identifier']
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py", line 57, in __getitem__
return self._session[key]
KeyError: 'user_identifier'
[2018/07/08 20:54:03] WebSocket HANDSHAKING / [127.0.0.1:65478]
in consumer: AnonymousUser8883761c-f673-413e-82e2-413d1cbb17e3
[2018/07/08 20:54:03] WebSocket CONNECT / [127.0.0.1:65478]
Internal Server Error: /get_spotify_link/
Traceback (most recent call last):
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/myusername/PycharmProjects/artist_notify/core/views.py", line 442, in get_spotify_link
user_identifier = request.session['user_identifier']
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py", line 57, in __getitem__
return self._session[key]
KeyError: 'user_identifier'