nonrel在GAE上发出信号

nonrel在GAE上发出信号

本文介绍了Django使用django-nonrel在GAE上发出信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的GAE项目上使用django-nonrel.我的要求是,在我的应用程序中,一次只能有一个用户使用给定的用户名登录.我尝试实施以下建议的方法:在Django应用中,每个用户只能进行一次并发登录如何我可以检测到来自不同位置的Django Web应用程序的多次登录吗?但是问题是这两种方法都可以在开发服务器上使用,但不适用于Google App引擎.因此,我选择了django-signals作为替代方法.我创建了一个post_login信号,该信号会将每个登录用户的用户名存储在数据库的Visitor表中.每次注销时,其他信号post_logout将从该表中删除该用户.部分代码如下:

I am using django-nonrel for my project on GAE. 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?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. I created one post_login signal which will store the username for every login user in a table Visitor in database. On every logout,other signal post_logout will remove the user from this table.The part of codes are as:

#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)

请注意,在Google App Engine上运行我的应用程序时出现以下错误.错误:服务器错误服务器遇到错误,无法完成您的请求.

Please note that I am getting the following error while running my application on google app engine.Error: Server ErrorThe 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.

您不应该像正在做的那样编辑django框架.请勿触摸django.contrib.auth

1.

You should not be editing the django framework like you are doing. Don't touch the files inside 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

不确定您的实际错误是因为没有显示它(如果这是一个开发环境,则设置DEBUG = True以获得更好的堆栈跟踪),但是通过查看您的代码,您并没有在信号中正确地获取参数.处理程序.它应该看起来像这样:

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. 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)

这篇关于Django使用django-nonrel在GAE上发出信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 23:25