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

发布于 2024-10-18 03:49:29 字数 1726 浏览 3 评论 0原文

我在 GAE 上的项目中使用 django-nonrel。我的要求是,在我的应用程序中,一次只能有一个用户使用给定的用户名登录。我尝试实施以下建议的方法: 在 django 应用程序中仅允许每个用户同时登录一次 和 如何我可以检测从不同位置多次登录 Django Web 应用程序吗? 但问题是这两种方法都适用于开发服务器,但不适用于谷歌应用程序引擎。所以我改用 django-signals 作为我的替代方法。我创建了一个 post_login 信号,它将每个登录用户的用户名存储在数据库的 Visitor 表中。每次注销时,其他信号 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)

请注意,在 google app engine 上运行我的应用程序时,我收到以下错误。 错误:服务器错误 服务器遇到错误,无法完成您的请求。

另外,我无法登录应用程序的管理部分。请帮助我找到正确的方法来实现此要求,或者让我知道我哪里做错了。 感谢您耐心阅读这个巨大的问题描述:-)

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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热风软妹 2024-10-25 03:49:29

1.

您不应该像现在这样编辑 django 框架。不要触摸 django.contrib.auth 中的文件

如果您希望在某人登录后发送信号,请在您登录此人的视图中发送信号

2.

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

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)

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

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. 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文