django-restful框架中的Null会话


Django rest框架

一旦用户登录,我就会设置会话密钥

request.session['id'] = 1

然后当我尝试在类视图中访问它时,比如

id = request.session.get('id', 0)

如果端点调用是从浏览器发出的,则为null [0],但如果端点调用来自邮递员,则返回1。。。

我需要你的指导。。。

Session cookies are browser-specific - they are only sent back to the server if the request comes from the same browser that received the cookie in the first place.
Postman is not a browser - it is simply making raw HTTP requests. It will not send any cookies back, so of course you will not see session data.
This behavior is entirely expected and by design. Sessions are only persisted per-browser.
If you want to make API calls and authenticate them, you should use token authentication instead, which can be passed in the Authorization header of requests and is not tied to a browser session.

最新更新