Django信号重定向 [英] Django signals redirect

查看:53
本文介绍了Django信号重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户登录时,我检查它是否属于特定组,如果是这样,我将重定向到特定页面,这是我的代码apps.py:

when a user logs in,I check if it belongs to a specific group, if this is true I do a redirect to a specific page, here is my code apps.py:

def OperatorRedirect(sender, user, request, **kwargs):
    from struttura_employee.models import EmployeeUser, EmployeeGroup

    u = get_object_or_404(EmployeeUser,username=user.username)
    groups = u.get_groups()
    g = get_object_or_404(EmployeeGroup,long_name="Operatore")
    if g in groups:
        print("sei un operatore")
        return HttpResponseRedirect(reverse('qrs_machine_panel:asset_choice', args=[]))

class QrsofmanMachinePanelConfig(AppConfig):
    name = 'qrsofman_machine_panel'
    print("ciao")
    user_logged_in.connect(OperatorRedirect)

问题是HttpResponseRedirect被忽略有什么想法吗?

the problem is that HttpResponseRedirect is being ignored any ideas?

推荐答案

这不是您可以在信号中执行的操作.这些用于诸如在发生特定操作时更新数据库行或发送电子邮件之类的事情.它们不能以任何方式影响响应,因为这是由视图管理的.

This isn't a thing that you can do in a signal. Those are for things like updating a database row, or sending an email, when a specific action happens. They can't affect the response in any way, because that is managed by the view.

您的重定向代码需要进入登录视图本身;只需在调用 auth.login()后进行检查.

Your redirection code needs to move into the login view itself; just do the check after you call auth.login().

这篇关于Django信号重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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