简体   繁体   English

password_set信号在Django allauth中不起作用

[英]password_set signal not working in Django allauth

What I am trying to do ? 我想做什么?

I am trying to redirect a new user to login dialog after user sets a password for the first time. 我试图在用户首次设置密码后将新用户重定向到登录对话框。 (I am doing this because the movement user sets a password Django implicitly logout the user) (我这样做是因为移动用户设置了密码Django隐式注销用户)

What is the problem ? 问题是什么 ?

For some reason the password_set signal doesn't seem to work. 由于某些原因, password_set信号似乎不起作用。 ie the sender function loginAfterPassChange doesn't get executed. 即发送器函数loginAfterPassChange不执行。

My Code: 我的代码:

How do I set password 如何设定密码

views.py: views.py:

@receiver(user_logged_in)
def getFirstTimePass(user, request, **kwargs): #this works
    if user.profile.provider != '' and user.profile.firstTimeLogin:
        user.profile.firstTimeLogin = False
        user.profile.save()
        raise ImmediateHttpResponse(render(request, 'index.html', {'type': user.profile.provider, 'email': user.email}))

@receiver(password_set)
def loginAfterPassChange(request, user, **kwargs): #this doesn't work
    data = {'msg': 'Please login with your new password.'}
    return HttpResponse(data)

def setPassword(request): #this works
    data = {'errMsg': 'Something went wrong.'}
    if not request.user.has_usable_password():
            password = request.POST.get('password')         
            request.user.set_password(password)
            request.user.save()
            data['errMsg'] = ''
    return JsonResponse(data)   

urls.py: urls.py:

from django.conf.urls import url
from .import views

urlpatterns = [
url(r'^updatePro', views.setPassword, name='updatePro') 
]

models.py: models.py:

class Profile(models.Model):
   user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)  
   provider = models.CharField(max_length=256, default='')
   firstTimeLogin = models.BooleanField(default=True)

if user.profile.provider != '' Check to see if user is logged in with social account or local account. if user.profile.provider != ''检查用户是否使用社交帐户或本地帐户登录。

You can't use a signal for this. 您不能为此使用信号。 Django doesn't do anything with the return value from a signal, so it won't redirect. Django对信号的返回值不做任何事情,因此它不会重定向。 This simply isn't what signals are for. 这根本不是信号的目的。 You should have all this logic in the view. 您应该在视图中拥有所有这些逻辑。

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

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