Opened 6 years ago

Last modified 6 years ago

#30045 closed Bug

"can't concat str to bytes error" on SessionMiddleware with python 3 — at Initial Version

Reported by: fmdra Owned by: nobody
Component: contrib.sessions Version: 2.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I recently moved to python 3 and django 2.1 (from 2.7.x and 1.11.x, respectively).

I'm hitting this issue:
"can't concat str to bytes error"

I believe the following line needs to be changed from:

return base64.b64encode(hash.encode() + b":" + serialized).decode('ascii')

to:

return base64.b64encode(hash.encode() + b":" + serialized.encode()).decode('ascii')

Below is a redacted traceback:
---
Environment:

Request Method: GET
Request URL: <redacted>

Django Version: 2.1.4
Python Version: 3.6.6
Installed Applications:
['django.contrib.admin',

'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'<redacted>']

Installed Middleware:
['django.middleware.security.SecurityMiddleware',

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

Traceback:

File "<redacted>/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner

  1. response = get_response(request)

File "<redacted>/lib/python3.6/site-packages/django/utils/deprecation.py" in call

  1. response = self.process_response(request, response)

File "<redacted>/lib/python3.6/site-packages/django/contrib/sessions/middleware.py" in process_response

  1. request.session.save()

File "<redacted>/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py" in save

  1. obj = self.create_model_instance(data)

File "<redacted>/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py" in create_model_instance

  1. session_data=self.encode(data),

File "<redacted>/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py" in encode

  1. return base64.b64encode(hash.encode() + b":" + serialized).decode('ascii')

Exception Type: TypeError at <redacted>
Exception Value: can't concat str to bytes
---

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top