我有一个Django应用程序,该应用程序使用Cookie会话将数据从一个模板/函数传递到另一个模板/函数。
我遇到的问题是,在将任何一个基于文件的cookie设置为基于DB的cookie时,它都可以在本地工作,但是当我尝试在数字海洋开发服务器中设置相同的会话时,它由于某些未知原因而失败。
文件
会话设置
。
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
是使用
SESSION_ENGINE = 'django.contrib.sessions.backends.file'
SESSION_FILE_PATH = os.path.join(BASE_DIR, "session")
事务的
def pre_login(request):
request.session['pre_login'] = data
....
def index(request):
user_data = request.session['pre_login']
....
以下
但是,在检查相同代码的会话时,我的开发服务器没有什么。
I have a Django app which uses cookie session to pass data from one to another template/functions.
The problem that I face is that while setting the cookie with either file based, DB based it worked in local perfectly, but while I try set the same session in Digital Ocean development server, it failed for some unknown reason.
Here are file which transaction with session
settings.py
Attempt 1:
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
Attempt 2:
SESSION_ENGINE = 'django.contrib.sessions.backends.file'
SESSION_FILE_PATH = os.path.join(BASE_DIR, "session")
views.py
def pre_login(request):
request.session['pre_login'] = data
....
def index(request):
user_data = request.session['pre_login']
....
While checking the cookie session in browser, it creates a sessionid for Localhost.
But when checking session for the same code, there is nothing for my development server.
发布评论
评论(1)
您正在请求中设置值。当您尝试在另一个视图中获取它时,您就会不在范围内。
You're setting the value in the request. When you try to get it in another view you're out of scope.
https://docs.djangoproject.com/en/4.0/topics/http/sessions/#using-sessions-out-of-views