Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'Request' object has no attribute 'session' #62

Closed
Paladiamors opened this issue May 16, 2020 · 7 comments
Closed

AttributeError: 'Request' object has no attribute 'session' #62

Paladiamors opened this issue May 16, 2020 · 7 comments

Comments

@Paladiamors
Copy link
Contributor

I used the demo code provided:

from sanic import Sanic
from sanic.response import text
from sanic_session import Session, InMemorySessionInterface

app = Sanic()
session = Session(app, interface=InMemorySessionInterface())

@app.route("/")
async def index(request):
    # interact with the session like a normal dict
    if not request['session'].get('foo'):
        request['session']['foo'] = 0

    request['session']['foo'] += 1

    return text(request['session']['foo'])

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, auto_reload=False)

And I get the following error:

Traceback (most recent call last):
  File "/home/justin/Coding/LearnReact/SanicBasics/pybin/lib/python3.8/site-packages/sanic/app.py", line 942, in handle_request
    response = await self._run_request_middleware(
  File "/home/justin/Coding/LearnReact/SanicBasics/pybin/lib/python3.8/site-packages/sanic/app.py", line 1304, in _run_request_middleware
    response = await response
  File "/home/justin/Coding/LearnReact/SanicBasics/pybin/lib/python3.8/site-packages/sanic_session/__init__.py", line 34, in add_session_to_request
    await self.interface.open(request)
  File "/home/justin/Coding/LearnReact/SanicBasics/pybin/lib/python3.8/site-packages/sanic_session/base.py", line 120, in open
    request[self.session_name] = session_dict
TypeError: 'Request' object does not support item assignment

It seems that this code is expecting a dict like object to set the session data.

@Paladiamors
Copy link
Contributor Author

This test was done with sanic version 20.3.0 and sanic_session 0.6.0

@Paladiamors
Copy link
Contributor Author

I have pushed a potential fix for this problem, please review: #63

@ashleysommer
Copy link

@Paladiamors
This is because Sanic 20.3.0 removed the deprecated arbitrary context attributes, in favor of the request.ctx mechanism introduced in Sanic 19.9.

@Paladiamors
Copy link
Contributor Author

Thanks I've made a fix to use the new ctx mechanism. It seems that updating these libraries with backwards compatability will be important to work with other tools that use the old response["session"] mechanism:

sanic-org/sanic#1854 (comment)

@Paladiamors
Copy link
Contributor Author

Closed as the pull request has been merged

@xen
Copy link
Collaborator

xen commented May 18, 2020

Thank you for your contribution. I will make release ASAP.

@Paladiamors
Copy link
Contributor Author

@xen thanks for accepting the change and performing the release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants