简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM