简体   繁体   English

刷新页面时如何注销应用程序? 在 Django

[英]How to Logout the App when Refresh the page ? In Django

This Is My code of login in Django Application这是我在 Django 应用程序中的登录代码

from django.shortcuts import redirect, render
from django.contrib.auth import authenticate,logout,login
from django.contrib.auth.decorators import login_required
from SedHelper.settings import LOGIN_URL
from .models import HelperApps
# Create your views here.

def Login(request):
    if request.method == 'GET':
        if  request.user.is_authenticated:
            logout(request)
        return render(request,'login.html')
    elif request.method == 'POST':
        username=request.POST['username']
        password=request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user)
            return render(request,'login.html',context=error)
            # No backend authenticated the credentials
def logout_view(request):
    print(request.user.is_authenticated)
    if  request.user.is_authenticated:
        logout(request)
     return redirect(Login)

def home(request):
    #load all data from db(10)
    if  request.user.is_authenticated:
         posts=HelperApps.objects.all()[:11]
         return render(request,'dashboard.html',{'posts':posts})
    else:return redirect(Login)

I just wanted to Logout when ever someone refresh the Page.In entire Application where ever someone refresh the the page it should logout immidiately.Anyone please.我只是想在有人刷新页面时注销。在整个应用程序中,有人刷新页面时,它应该立即注销。任何人请。 i am also new to stackoverflow Please try to ignore the mistakes.我也是stackoverflow的新手请尽量忽略错误。

Create a Middleware, and write this function:创建一个中间件,并编写此 function:

def logout_when_refresh(request):
    logout(request)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM