简体   繁体   English

Django使用django-nonrel在GAE上发出信号

[英]Django signals on GAE with django-nonrel

I am using django-nonrel for my project on GAE. 我在GAE项目中使用django-nonrel。 My requirement is that in my application at a time only one user should login with the given username. 我的要求是,在我的应用程序中,一次只有一个用户应该使用给定的用户名登录。 I tried to implement the following suggested approaches: Allow only one concurrent login per user in django app and How can I detect multiple logins into a Django web application from different locations? 我尝试实现以下建议的方法: 在django应用程序中每个用户只允许一次并发登录如何从不同位置检测到多个登录到Django Web应用程序? But the problem is that both of the approaches working on the development server but didn't work on google app engine. 但问题是这两种方法都在开发服务器上工作,但在谷歌应用引擎上无效。 So I switched to django-signals as my alternate approach. 所以我切换到django信号作为我的替代方法。 I created one post_login signal which will store the username for every login user in a table Visitor in database. 我创建了一个post_login信号,它将每个登录用户的用户名存储在数据库中的表Visitor中。 On every logout,other signal post_logout will remove the user from this table.The part of codes are as: 在每次注销时,其他信号post_logout将从该表中删除用户。部分代码如下:

#signals.py
post_login = django.dispatch.Signal(providing_args=['request', 'user'])
post_logout = django.dispatch.Signal(providing_args=['request', 'user'])
#models.py
def login_handler(sender,user, **kwargs):
    try:
        result=Visitor.objects.get(user=user)
        print "You already have login with your name"
    except:
        visitor=Visitor()
        visitor.user=user
        visitor.save()
post_login.connect(login_handler)


def logout_handler(sender,user, **kwargs):
    try:
        result=Visitor.objects.get(user=user)
        result.delete()
    except:
        return False
post_logout.connect(logout_handler)

#django.contrib.auth.__init.py__
def login(request):
 :
 user_logged_in.send(sender=user.__class__, request=request, user=user)
  post_login.send(sender=None,request=request, user=user)

def logout(request):
:
user_logged_out.send(sender=user.__class__, request=request, user=user)
post_logout.send(sender=None,request=request, user=user)

Please note that I am getting the following error while running my application on google app engine. 请注意,我在谷歌应用引擎上运行我的应用程序时收到以下错误。 Error: Server Error The server encountered an error and could not complete your request. 错误:服务器错误服务器遇到错误,无法完成您的请求。

Also I am not able to login into Admin part of the application. 此外,我无法登录到应用程序的管理员部分。 Please help me to find right approach to implement this requirement or let me know where I am doing wrong. 请帮我找到正确的方法来实现这个要求或让我知道我做错了什么。 Thanks for your patience for reading this huge problem description :-) 感谢您耐心阅读这个巨大的问题描述:-)

1. 1。

You should not be editing the django framework like you are doing. 你不应该像你一样编辑django框架。 Don't touch the files inside django.contrib.auth 请勿触摸django.contrib.auth中的文件

If you wish to send a signal after someone is logged in, then send the signal in your view where you log the person in 如果您希望在某人登录后发送信号,请在您查看该人员的视图中发送信号

2. 2。

Not sure what your actual error is because you are not displaying it (if this is a dev environment set DEBUG = True to get a better stack trace) But by lookingat you code, you are not grabbing the arguments correctly in the signal handler. 不确定你的实际错误是什么,因为你没有显示它(如果这是一个开发环境设置DEBUG = True以获得更好的堆栈跟踪)但是通过查看代码,你不是在信号处理程序中正确地获取参数。 It should look more like this: 它应该看起来更像这样:

def login_handler(sender, **kwargs):
    try:
        user = kwargs['user']
        request = kwargs['request']
        result=Visitor.objects.get(user=user)
        print "You already have login with your name"
    except:
        visitor=Visitor()
        visitor.user=user
        visitor.save()
post_login.connect(login_handler)

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

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