Added to log out the current user.

This commit is contained in:
依瑪貓 2020-07-06 23:04:14 +08:00
parent de679cf91a
commit 68be53e212
2 changed files with 22 additions and 2 deletions

View File

@ -65,6 +65,9 @@ def digest_login_required(function=None):
def _wrapped_view(request, *args, **kwargs):
if request.user.is_anonymous:
return HttpResponse(status=401)
if "logout" in request.session:
del request.session["logout"]
return HttpResponse(status=401)
return view_func(request, *args, **kwargs)
return _wrapped_view
if function:

View File

@ -1,3 +1,20 @@
from django.shortcuts import render
from django.contrib.auth import logout
from django.shortcuts import redirect
from django.views.decorators.http import require_POST
# Create your views here.
@require_POST
def logout_view(request):
"""The view to log out a user.
Args:
request (HttpRequest): The request.
Returns:
HttpRedirectResponse: The redirect response.
"""
logout(request)
if "next" in request.POST:
request.session["logout"] = True
return redirect(request.POST["next"])
return redirect("/")