简体   繁体   English

会话数据在本地服务器中工作,而不是在服务器中 - Django

[英]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.

相关问题 OperationalError at / no such table: 公共服务器上的 django_session 错误,站点在本地服务器上工作正常 - OperationalError at /no such table: django_session error on public server, site works fine on local server 本地和在线服务器之间的Django数据同步 - Django Data synchronisation between local and online server django 密码重置在服务器上不起作用,但在本地计算机上起作用 - django password reset does not work on server but works on local machine django order_by 不在服务器上工作,但在本地机器上工作正常 - django order_by not working on server but works fine on local machine Django Selenium 测试在本地 Docker 容器中有效,但在 Z2E54334C0A5CE2E3E3E5A584Z5DF3 服务器上无效 - Django Selenium test works in local Docker container but not on Jenkins server selenium 在本地工作,而不是在 azure 服务器上工作 - selenium works on local and not on azure server 如何使用 React-native 访问 Django 本地服务器数据? - How to access Django local server data with React-native? 如何将数据从 ESP8266 发送到 Django 本地服务器 - How to send data from ESP8266 to Django local server 本地Django服务器重定向到Sentry服务器 - Local Django server redirects to Sentry server Django本地服务器停止工作 - Django local server stopped working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM