I have a django rest framework. And I am using
version 5.0.6
and
djangorestframework==3.14.0
And I try to implement the logout function.
What I already tried? I followed several recourses. For example this one:
/questions/58283277/user-logout-by-session-in-django-rest-framework
So my logout looks:
class LogoutView(APIView):
def get(self, request, format=None):
# Get the user's token
# request.user.auth_token.delete()
logout(request)
return Response(status=status.HTTP_200_OK)
url:
path('logout/',views.LogoutView.as_view(), name='logout'),
and part of settings.py file:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'drf_yasg',
'corsheaders',
'rest_framework.authtoken',
'django_filters',
"accounts",
"core",
'drf_spectacular',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
So when I loged in and then I choose for logout then the user will be redirected to url:
http://127.0.0.1:8000/api-auth/logout/?next=/api/
And I got this message:
This page isn’t working
If the problem continues, contact the site owner.
HTTP ERROR 405
And I also tried with post.
And I still don’t understand which HTTP call you need for logout? Is it get or post? Because in the link I have found it is written with a get option.
Question: how to build logout function in django rest framework?