关闭浏览器时运行一个函数 Django [英] Run a function when the browser is closed Django

查看:27
本文介绍了关闭浏览器时运行一个函数 Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我阅读的内容,无法了解用户是否通过 Django 关闭了浏览器或选项卡.理想情况下,我认为关闭浏览器或选项卡应该没有问题,因为用户会自动退出.

Based from what I've read there is no way of learning if the browser or a tab is closed by the user through Django. Ideally I think there should be no problem in closing the browser or a tab because the user is automatically logged out.

但是,我的问题是我已经覆盖了我的注销视图,因此 Django 会执行一些与登录用户相关的模型的功能(例如保存时间戳等).每当用户通过关闭浏览器注销时,Django 不会启动这些功能.当用户通过注销"正常注销时,它会运行.按钮.

However, my problem is I have overridden my log-out view so Django does some functions (such as saving a time-stamp, and etc.) regarding the models relating to the logged in user. Django does not fire up these functions whenever the user logs out through closing the browser. It runs though when the user logs out normally through the "Log-Out" button.

这是我覆盖的注销视图:

This is my overridden log-out view:

def logout(request):
    reset_logged_acc(request) #do something
    auth_logout(request) #custom log-out
    return redirect('home')

我在某处读到过,我可以执行一些 ajax 请求,以及不知道用户是否还在的地方.我认为这是可能的,但如果没有任何结果,我会将这个选项作为最后的手段.

I've read somewhere that I can do some ajax requests and what not to know if the user is still there. I think its possible but I'm putting that option up as a last resort if nothing comes up.

是否有其他方法可以实现这一点?

Are there any alternative ways to implement this?

推荐答案

找到了解决这个问题的方法,虽然不太好.

Found a work around for this though not pretty.

基本上,我刚刚创建了一个 request.session['window_num'] 表示从 Django 生成的窗口数.

Basically, I just created a request.session['window_num'] which represents the number windows generated from Django.

所以这意味着对于每个 GET 请求,Django 通过 add_window(request) 增加会话变量.

So this means for every GET request Django increments the session variable via add_window(request).

为了处理页面的删除或关闭,我只是使用了 $(window).on('unload', function() {}) 中包含的 navigator.sendBeacon 函数; Javascript 中的函数向 Django 发送请求.Django 然后通过 delete_window(request) 递减会话变量.

To handle the deletion or closing of pages, I just used navigator.sendBeacon function enclosed by the $(window).on('unload', function() {}); function in Javascript to send a request to Django. Django then decrements the session variable via delete_window(request).

然后我在函数关闭之前添加了一个 if 语句来检查会话变量是否变为 0.如果变为 0,它会调用我覆盖的注销函数.

Then I added an if statement just before the function closes to check if the session variable goes to 0. If it goes to 0, it calls my overridden log-out function.

我承认这不是最好的代码,所以我仍然愿意接受任何改进或替代方案.刚刚发布这个是因为它有效.

I admit this is not the best code for this so I'm still open for any improvements or alternatives. Just posted this because it works.

每 n 秒尝试一次 ajax 请求,但无法在 Django 中实现.

Tried the ajax request every n seconds thing but can't implement it in Django.

这些是以下代码:

navigator.sendBeacon

$(window).on('unload', function() {
      var data = new FormData();        
      data.append('csrfmiddlewaretoken', '{{csrf_token}}');
      navigator.sendBeacon("{% url 'delete_window' %}",data);
  });

views.py

@csrf_exempt
def delete_window(request):    
    window_num = int(request.session['window_num'])
    window_num -= 1
    request.session['window_num'] = str( window_num )
    
    if int( request.session['window_num'] ) == 0:
        if request.user.is_authenticated:
            logout(request) #calls out overridden log-out function          

    data = {}
    return JsonResponse(data)

def add_window(request):
    if 'window_num' in list(request.session.keys()): #there are existing pages
        window_num = int(request.session['window_num'])
        window_num += 1 
        request.session['window_num'] = str( window_num )

    else: #first time load
        request.session['window_num'] = "1"

这篇关于关闭浏览器时运行一个函数 Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆