julianwachholz / django-guest-user

A Django app that allows visitors to interact with your site as a guest user without requiring registration.
https://django-guest-user.readthedocs.io/
MIT License
74 stars 10 forks source link

Why am I told to add django.contrib.sessions to INSTALLED_APPS? #11

Closed Westlifers closed 1 year ago

Westlifers commented 1 year ago

Hello, I'm trying to apply this package in my project, but I'm stuck here When I visit an API with @allow_guest_user above the get method:

class GuestUserLogin(GenericAPIView):
    permission_classes = [permissions.AllowAny]

    @allow_guest_user
    def get(self, request):
        username = request.user.username
        return JsonResponse({'detail': f'Guest user login success!{username}'})

and if I visit this API by router path('guestlogin/', GuestUserLogin.as_view()),, it returns an error:

Internal Server Error: /guestlogin/
Traceback (most recent call last):
  File "F:\learn\cubewall\venv\Lib\site-packages\asgiref\sync.py", line 534, in thread_handler
    raise exc_info[1]
  File "F:\learn\cubewall\venv\Lib\site-packages\django\core\handlers\exception.py", line 42, in inner
    response = await get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\asgiref\sync.py", line 534, in thread_handler
    raise exc_info[1]
  File "F:\learn\cubewall\venv\Lib\site-packages\django\core\handlers\base.py", line 253, in _get_response_async
    response = await wrapped_callback(
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\asgiref\sync.py", line 479, in __call__
    ret: _R = await loop.run_in_executor(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\asgiref\sync.py", line 538, in thread_handler
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\django\views\decorators\csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\django\views\generic\base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "F:\learn\cubewall\venv\Lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "F:\learn\cubewall\venv\Lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\guest_user\decorators.py", line 28, in wrapper
    maybe_create_guest_user(request)
  File "F:\learn\cubewall\venv\Lib\site-packages\guest_user\functions.py", line 21, in maybe_create_guest_user
    assert hasattr(
AssertionError: Please add 'django.contrib.sessions' to INSTALLED_APPS.

However, 'django.contrib.sessions' do exist in INSTALLED_APPS:

INSTALLED_APPS = [
    'daphne',
    'wall',
    'simpleui',
    'rest_framework',
    'guest_user',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

I tried to move it to the top or the bottom of INSTALLED_APPS but neither worked. If I remove the decorator @allow_guest_user, it worked well(with a real user logged in)

My Django version: 4.2.4 Python version: 3.11.1

Westlifers commented 1 year ago

sorry, I didn't fully understand the documentation. This can be solved by using AllowGuestUserMixin:

class GuestUserLogin(AllowGuestUserMixin, GenericAPIView):
    permission_classes = [permissions.AllowAny]

    def get(self, request):
        username = request.user.username
        return JsonResponse({'detail': f'Guest user login success!{username}'})

my bad :(

julianwachholz commented 1 year ago

Hi @Westlifers thank you for creating this issue! Glad you were able to find the issue yourself. I think I can improve the error message a bit in this case when using a decorator on a view class in the future.