[英]Session data works in local server, not in server - Django
I have a Django app which uses cookie session to pass data from one to another template/functions.我有一个 Django 应用程序,它使用 cookie 会话将数据从一个模板/函数传递到另一个模板/函数。
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.我面临的问题是,虽然使用基于文件的 cookie 设置 cookie,但它在本地完美地工作,但是当我尝试在 Digital Ocean 开发服务器中设置相同的会话时,它由于某种未知原因而失败。
Here are file which transaction with session这是带有会话的事务的文件
settings.py设置.py
Attempt 1:尝试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:尝试2:
SESSION_ENGINE = 'django.contrib.sessions.backends.file'
SESSION_FILE_PATH = os.path.join(BASE_DIR, "session")
views.py视图.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.在浏览器中检查 cookie 会话时,它会为 Localhost 创建一个 sessionid。 But when checking session for the same code, there is nothing for my development server.但是当检查相同代码的会话时,我的开发服务器什么都没有。
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 https://docs.djangoproject.com/en/4.0/topics/http/sessions/#using-sessions-out-of-views
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.