Django Session KeyError

1,140 views
Skip to first unread message

Guy NANA

unread,
Feb 24, 2020, 5:17:25 PM2/24/20
to Django users
I have an angular frontend app which send file to django backend which data is setting in django session. After I send a httprequest to django backend to make ML tratements on that data and get the results. But I've a 500 sever error: keyerror 'ts_dataset_copy': KeyError: 'ts_dataset_copy'
[24/Feb/2020 18:43:46] "GET /cv_classification/5/FOTS/283/None/0/0 HTTP/1.1" 500 78264. Here are my django code:

Firstly I upload timeseries dataset file from angular frontend (All thing is ok)   
@csrf_exempt
   
def upload_local_dataset(request):
       
if request.method == 'POST':
            dataset
= pd.read_csv(request.FILES.get('datasetfilepath'), header=None, index_col=None)
            request
.session['ts_datset'] = dataset.to_json(orient='values')
            request
.session['ts_dataset_copy'] = dataset.to_json(orient='values')
   
           
return HttpResponse(dataset.to_json(orient='values'))



# second httrequest that throws a server internal error

    def cv_classification(request, kfolds, dissimilarity_func, windows_length=0, noisy_law="", mu=0,
   
        std
=0):
            noisy_law
= noisy_law.lower()
            df
= pd.read_json(request.session['ts_dataset_copy'], orient='values')
            predictions
= cv_classify(df, kfolds, dissimilarity_func, windows_length, noisy_law, mu, std)
           
return JsonResponse(predictions, safe=False)



Thanks for your help!

Pankaj Sharma

unread,
Feb 25, 2020, 7:29:53 AM2/25/20
to Django users
Make sure your line request.session['ts_dataset_copy'], print anything above it, also make sure you are using POST method in forms (method = "post") 

Guy NANA

unread,
Feb 25, 2020, 9:16:15 AM2/25/20
to Django users
I don't understand the first part of your answer : Make sure your line request.session['ts_dataset_copy'], print anything above it.
 For sending dataset file from angular app to django, I've used a post request.

The matter is that when I send the second http crosss-origin from angular app to my django app the previously session's elements that I've stored doesn't exist anymore. (I checked the session keys in the upload_local_dataset and receive In upload_local_dataset Session's keys : dict_keys(['ts_dataset', 'ts_dataset_copy']) but in the second views I received an empty dict In cv_classification Session's keys : dict_keys([]) so pandas' read_json throws an error ValueError: Invalid file path or buffer object type:

How can I save the state of session between two http cross-origin from angular app to django app ?

Juan Pablo Romero Bernal

unread,
Feb 25, 2020, 8:23:59 PM2/25/20
to django...@googlegroups.com
Hi,

In your Django application, session middleware is enabled ? [1]


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c0d3a833-15f2-415f-b4e0-c05951f523f0%40googlegroups.com.


--
Juan 

Guy NANA

unread,
Feb 25, 2020, 8:56:29 PM2/25/20
to Django users
Hi!
Yes, it is.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tsanalysisapp.apps.TsanalysisappConfig',

# Django Rest Framework - DRF
'rest_framework',
# Django Rest Pandas - DRP
'rest_pandas',

# CORS
'corsheaders',
]

MIDDLEWARE = [
# CORS
'corsheaders.middleware.CorsMiddleware',

'django.middleware.common.CommonMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

Le mercredi 26 février 2020 02:23:59 UTC+1, juanblo a écrit :
Hi,

In your Django application, session middleware is enabled ? [1]


To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.


--
Juan 

Naveen Arora

unread,
Feb 26, 2020, 2:38:08 AM2/26/20
to Django users
Hi, 

please post the output of debugging the above, print after and before in these views and also check is something else is working using request.session. 
@csrf_exempt
    
def upload_local_dataset(request):
        
if request.method == 'POST':
            dataset 
= pd.read_csv(request.FILES.get('datasetfilepath'), header=None, index_col=None)
            request
.session['ts_datset'] = dataset.to_json(orient='values')
            print(dataset.to_json(orient='values'))
            request.session['ts_dataset_copy'] = dataset.to_json(orient='values')
            print(request.session['ts_dataset_copy'])
            return HttpResponse(dataset.to_json(orient='values'))
Post output of this plus the below one
def cv_classification(request, kfolds, dissimilarity_func, windows_length=0, noisy_law="", mu=0,std=0):
            noisy_law 
= noisy_law.lower()
            print(request.session['ts_dataset_copy'])
            df = pd.read_json(request.session['ts_dataset_copy'], orient='values')
            predictions 
= cv_classify(df, kfolds, dissimilarity_func, windows_length, noisy_law, mu, std)
            
return JsonResponse(predictions, safe=False)

Guy NANA

unread,
Feb 26, 2020, 3:56:22 AM2/26/20
to django...@googlegroups.com
Hi! Please find bellow the outputs. When I was using sessions stored in cache (specially in Memcached) all things were working good until I wanted to upload a data set with larger volume of data : it wasn't be possible to put them in cache.


Training dataset uploaded :  Coffee
training dataset saved in django session :

 [[0,-0.51841899,-0.48588363,-0.50500747,-0.56018294,-0.63629941,-0.75322902,-0.82722915,-0.85976467,-0.90632072,-0.92379648,-0.93321222,-0.93442926,-0.92078716,-0.93657191,-0.95599685,-0.95934101,-0.96940599,-0.9824055,-0.97659952,-0.96252576,-0.97031893,-0.98199237,-0.9874616,-1.0152202,-1.0480611,-1.0592421,-1.0498854,-1.0467321,-1.0643986,-1.0877935,-1.0993351,-1.0850813,-1.0641912,-1.0545229,-1.044503,-1.0236367,-0.99421712,-0.96371982,-0.92237217,-0.86616903,-0.81351574,-0.77266759,-0.72343976,-0.66424331,-0.63424598,-0.6280451,-0.60469363,-0.56829781,-0.53778323,-0.50569966,-0.4612259,-0.38892741,-0.29296669,-0.20056328,-
...
582,-1.7619773,-1.7640397,-1.7661713,-1.7689397,-1.7722404,-1.7751282,-1.7766233,-1.7782533,-1.7809291,-1.7835006,-1.7858671,-1.7878936,-1.7896332,-1.791887,-1.7937475,-1.795334,-1.7968732]]


training dataset copy saved in django session :
     0         1         2         3         4         5         6         7         8    ...       278       279       280       281       282       283       284       285       286
0     0 -0.518419 -0.485884 -0.505007 -0.560183 -0.636299 -0.753229 -0.827229 -0.859765  ... -1.922313 -1.924212 -1.926997 -1.928721 -1.930026 -1.932301 -1.933631 -1.934963 -1.936007

23    1 -0.654035 -0.634715 -0.625911 -0.650577 -0.710112 -0.793933 -0.876500 -0.917085  ... -1.758149 -1.760285 -1.763167 -1.766203 -1.768161 -1.769878 -1.771651 -1.772835 -1.774231
24    1 -0.675463 -0.617801 -0.619069 -0.664476 -0.751102 -0.841188 -0.900013 -0.937926  ... -1.733537 -1.736082 -1.738777 -1.741560 -1.743612 -1.745258 -1.747073 -1
50    1 -0.674712 -0.633369 -0.648089 -0.706044 -0.763404 -0.841210 -0.926266 -0.967801  ... -1.798162 -1.800553 -1.803785 -1.806106 -1.808189 -1.810410 -1.812302 -1.814185 -1.815227
51    1 -0.637021 -0.624313 -0.602822 -0.644700 -0.735494 -0.798365 -0.866908 -0.920802  ... -1.777194 -1.779707 -1.782322 -1.784423 -1.786802 -1.789479
55    1 -0.665276 -0.636800 -0.639735 -0.687703 -0.760849 -0.839653 -0.898612 -0.923578  ... -1.780929 -1.783501 -1.785867 -1.787894 -1.789633 -1.791887 -1.793747 -1.795334 -1.796873

[56 rows x 287 columns]
In upload_local_dataset Session'
s keys :  dict_keys(['ts_dataset', 'ts_dataset_copy'])
[26/Feb/2020 08:20:09] "POST /upload_dataset HTTP/1.1" 200 176589
[26/Feb/2020 08:20:13] "GET /uts_datasets HTTP/1.1" 200 35673
In cv_classification Session's keys :  dict_keys([])
Internal Server Error: /cv_classification/5/FOTS/283/None/0/0
Traceback (most recent call last):
  File "/home/proj-guyrostan1/STAGE/tsanalysiswebapp/backend/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/proj-guyrostan1/STAGE/tsanalysiswebapp/backend/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/proj-guyrostan1/STAGE/tsanalysiswebapp/backend/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/proj-guyrostan1/STAGE/tsanalysiswebapp/backend/djangobackend/tsanalysisapp/views.py", line 260, in cv_classification
    df = pd.read_json(request.session.get('
ts_dataset_copy'), orient='values')
  File "/home/proj-guyrostan1/STAGE/tsanalysiswebapp/backend/venv/lib/python3.7/site-packages/pandas/io/json/_json.py", line 569, in read_json
    path_or_buf, encoding=encoding, compression=compression
  File "/home/proj-guyrostan1/STAGE/tsanalysiswebapp/backend/venv/lib/python3.7/site-packages/pandas/io/common.py", line 224, in get_filepath_or_buffer
    raise ValueError(msg.format(_type=type(filepath_or_buffer)))
ValueError: Invalid file path or buffer object type: <class '
NoneType'>
[26/Feb/2020 08:20:29] "GET /cv_classification/5/FOTS/283/None/0/0 HTTP/1.1" 500 84649




--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/7pjz-2smXDQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ee6dd1d1-9567-46ba-a699-500d1e950788%40googlegroups.com.


--
Guy Rostan DJOUNANG  NANA
Élève Ingénieur en Informatique
-- Institut Africain d'Informatique ---
Certifié: LPIC-1 / OCA JAVA SE 8
Maîtrise de Mathématiques, Parcours type: Analyse - Algèbre - Géométrie
-- Faculté des Sciences / Université de Douala-Cameroun ---
Président du Comité des Étudiants de l'IAI
Juin 2017 - Octobre 2019
LinkedIn: Guy Rostan DJOUNANG NANA (www.linkedin.com/in/guy-rostan-djounang-nana/)

Guy NANA

unread,
Feb 26, 2020, 3:57:14 AM2/26/20
to Django users
Le mercredi 26 février 2020 08:38:08 UTC+1, Naveen Arora a écrit :
Hi,


please
 post the output of debugging the above, print after and before in these
 views and also check is something else is working using
request.session.
@csrf_exempt
    def upload_local_dataset(request):
        if request.method == 'POST':
            dataset = pd.read_csv(request.FILES.get('datasetfilepath'), header=None, index_col=None)
            request.session['ts_datset'] = dataset.to_json(orient='values')
            print(dataset.to_json(orient='values'))

            request.session['ts_dataset_copy'] = dataset.to_json(orient='values')
            print(request.session['ts_dataset_copy'])
            return HttpResponse(dataset.to_json(orient='values'))

Post output of this plus the below one
def cv_classification(request, kfolds, dissimilarity_func, windows_length=0, noisy_law="", mu=0,std=0):

            noisy_law = noisy_law.lower()
            print(request.session['ts_dataset_copy'])
            df = pd.read_json(request.session['ts_dataset_copy'], orient='values')
            predictions = cv_classify(df, kfolds, dissimilarity_func, windows_length, noisy_law, mu, std)
            return JsonResponse(predictions, safe=False)


On Tuesday, 25 February 2020 03:47:25 UTC+5:30, Guy NANA wrote:

Guy NANA

unread,
Feb 26, 2020, 4:00:05 AM2/26/20
to django...@googlegroups.com

Naveen Arora

unread,
Feb 26, 2020, 5:14:23 AM2/26/20
to Django users
this simple means request.session is empty in the view you are trying to access it. Can you test request.session for simple strings if it is working at all. need to checkout what exact problem is. For reference check out -https://docs.djangoproject.com/en/3.0/topics/http/sessions/
One more thing check if cookies are not disabled for localhost
Also , are you logged in ? Try after logging in as superuser.

Cheers,
Naveen Arora 
To unsubscribe from this group and all its topics, send an email to django...@googlegroups.com.

Guy NANA

unread,
Feb 26, 2020, 9:01:36 AM2/26/20
to Django users
I checked request.session for simple string (by adding request.session['func_views'] = 'upload_local_dataset' in the upload_local_dataset view) but it is always have an empty request.session in the second view.

Naveen Arora

unread,
Feb 27, 2020, 12:53:13 AM2/27/20
to Django users
Are you using SESSION_COOKIE_DOMAIN in your settings.py ?
Are you on local server only ?
Are you logged in while maintaining a session ?
Have you run the migrate command ? Without makemigrations and migrate properly session doesn't work !
Run manage.py syncdb from the command line.

References - 

If it still doesn't work after trying kindly share the github code. 

Peace
Naveen Arora
Reply all
Reply to author
Forward
0 new messages