[英]session not storing on browser in Django
I'm trying to build an app using React TSX
for the front-end side and Django
for the back-end side.我正在尝试构建一个应用程序,前端使用React TSX
,后端使用Django
。 I want to store the session on the browser using the SESSION_ENGINE=django.contrib.sessions.backends.signed_cookies
.我想使用SESSION_ENGINE=django.contrib.sessions.backends.signed_cookies
将 session 存储在浏览器上。 So, I want to store the sessions in a similar way of how PHP
or Apache
stores its sessions.因此,我想以类似于PHP
或Apache
存储其会话的方式存储会话。 I have tried a lot of ways but I am still not able to store the sessions, I keep getting the output SESSION IS NOW SET
from my code.我已经尝试了很多方法,但我仍然无法存储会话,我不断从我的代码中获取 output SESSION IS NOW SET
。 Any help will be great.任何帮助都会很棒。 Thanks谢谢
login.tsx
import Cookies from "js-cookie";
import axios, { AxiosResponse, AxiosError } from "axios";
..........
..........
axios.request({
url: "login/",
baseURL: "http://localhost:8000/",
method: "POST",
data: data,
timeout: 15000,
withCredentials: true,
headers: { "X-Requested-With": "XMLHttpRequest", "X-CSRFToken": Cookies.get("csrftoken") },
validateStatus: () => true
}).then((res: AxiosResponse): void => {
console.log(res.data);
});
login.py
from django.http import HttpResponse
def get(req):
req.session.modified = True
if req.session.get("user_id", False):
return HttpResponse("EXISTS: " + req.session.get("user_id")) <-- THIS OUTPUT IS NEVER BEING RETRIEVED
else:
req.session["user_id"] = "USER-ID"
req.session.save()
return HttpResponse("SESSION IS NOW SET") <-- THIS IS THE ONLY OUTPUT BEING RETRIEVED
settings.py
..........
..........
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders'
]
SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SECURE = True
SESSION_COOKIE_NAME = "sessioninfo"
CORS_ORIGIN_WHITELIST = [ "http://localhost:5000" ]
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_METHODS = ["GET","POST"]
CORS_ALLOW_HEADERS = [ "accept", "accept-encoding", "authorization", "content-type", "dnt", "origin", "user-agent", "x-csrftoken", "x-requested-with" ]
CSRF_COOKIE_SECURE = True
CSRF_COOKIE_HTTPONLY = True
..........
..........
I figured it out.我想到了。 All I had to do was my edit my settings.py
to this:我所要做的就是编辑我的settings.py
到这个:
..........
..........
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders'
]
SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"
SESSION_COOKIE_NAME = "user_session"
SESSION_COOKIE_HTTPONLY = True
SESSION_SAVE_EVERY_REQUEST = True
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
CORS_ORIGIN_WHITELIST = [ "http://localhost:5000" ]
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_METHODS = ["GET","POST"]
CORS_ALLOW_HEADERS = [ "accept", "accept-encoding", "authorization", "content-type", "dnt", "origin", "user-agent", "x-csrftoken", "x-requested-with" ]
CSRF_COOKIE_SECURE = True
CSRF_COOKIE_HTTPONLY = True
..........
..........
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.