Skip to content

APIRequestFactory Request object has no attribute session #8091

Discussion options

You must be logged in to vote

I fixed it by manually adding SessionMiddleware on the request, following this answer on a similar issue from stack overflow.

https://stackoverflow.com/questions/35659676/django-test-case-error-wsgirequest-object-has-no-attribute-session

I just created a mixin and made my test inherit it and then just call the add_session() method.

from django.contrib.sessions.middleware import SessionMiddleware

class SessionRequiredTestMixin:
    """Mixin for tests with request.session, cookies operations and requirements."""

    def add_session(self, request):
        """Add session object to request by using SessionMiddleware."""
        middleware = SessionMiddleware()
        middleware.process_req…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by edchelstephens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant