通过通道设置Django会话变量(http_session)

时间:2018-01-06 16:00:04

标签: python django session channel django-channels

根据Django频道文档,

    You get access to a user’s normal Django session using the http_session 
    decorator - that gives you a message.http_session attribute that behaves 
    just like request.session. You can go one further and use 
    http_session_user which will provide a message.user attribute as well as 
    the session attribute.

无论如何我可以像这样设置一个会话变量吗? 我确实尝试过,我得到一个错误,说NoneType不可迭代。

if 'username' not in message.http_session:
    message.http_session['username'] = 'temp'

换句话说,message.http_session返回NoneType。我尝试使用装饰器@http_session和@channel_session_user_from_http,但他们也没有帮助。

1 个答案:

答案 0 :(得分:0)

您只需设置变量即可。

foo ='bar'

它在整个会话过程中一直存在