Reputation: 91
I am working on a Django project and I'm encountering an issue with session variables. I set a session variable in one view, but when I try to access it in another view, it returns None.
Here's a simplified version of my code:
In my LoginVerification
view:
from rest_framework.views import APIView
from rest_framework.response import Response
class LoginVerification(APIView):
def post(request):
# ... login logic ...
# If login is successful, I store the AWS access key in the session
request.session['aws_access_key'] = 'your-access-key'
return Response({"message": "Login successful"})
In my BucketSize
view:
from rest_framework.views import APIView
from rest_framework.response import Response
class BucketSize(APIView):
def get(self, request):
# Here, I try to retrieve the AWS access key from the session
aws_access_key = request.session.get('aws_access_key')
if aws_access_key is None:
return Response({'status': 'error', 'error': 'No AWS access key found in session.'})
# ... remaining logic ...
return Response({'status': 'success', 'bucketsize': bucketsize, 'unit': unit})
In the BucketSize view, request.session.get('aws_access_key') is always returning None, even though I have set it in the LoginVerification view.
This is my settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'server',
'rest_framework',
"corsheaders",
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
I have checked the following:
The session variable is being set: The code in the LoginVerification view is running without errors.
- The session has not expired: I'm testing the BucketSize view immediately after the LoginVerification view.
- I have not cleared my cookies: I am consistently using the same browser session for testing.
- I am using the same browser session: I'm not switching browsers or going into incognito mode.
I would like to know why request.session.get('aws_access_key') is returning None in the BucketSize view, and how I can resolve this issue.
EDIT: This is my rest framework settings --
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny'
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
]
}
Upvotes: 0
Views: 964
Reputation: 507
to fix you issue you need to call the .save() method after assigning the key. You can do it as follows in your LoginVerification
:
EDIT:
from rest_framework.views import APIView
from rest_framework.response import Response
class LoginVerification(APIView):
def post(self, request): # self parameter was missing from the function (it can be anything but in python we usually use `self`)
# ... login logic ...
# If login is successful, I store the AWS access key in the session
request.session['aws_access_key'] = 'your-access-key'
request.session.save() # Need to save this
return Response({"message": "Login successful"})
Though it is not a good idea to store access keys in session variables
Upvotes: 0