我有一个django注册和工作。我想为它添加两个额外的功能,并且在理解登录过程的内部工作方面有点困难。
1)当用户点击激活电子邮件时,它会使帐户处于活动状态,但不会将用户登录,如何进行操作以便点击激活链接会使帐户处于活动状态并自动登录用户?这是我的activate
函数的样子 -
def activate(self, request, activation_key):
activated = RegistrationProfile.objects.activate_user(activation_key)
if activated:
signals.user_activated.send(sender=self.__class__,
user=activated,
request=request)
login (request, activated) ### if I try this line, it throws an error 'User'
### object has no attribute 'backend
return activated
更新 :我能够使用会话添加黑客来实现此功能。当然这不是理想的解决方案,但这就是我所拥有的 -
def register(self, request, **kwargs):
...
new_user.save()
request.session['username'] = username
request.session['password'] = password
return new_user
def activate(self, request, activation_key):
username = request.session['username']
password = request.session['password']
activated = RegistrationProfile.objects.activate_user(activation_key)
if activated:
signals.user_activated.send(sender=self.__class__,
user=activated,
request=request)
user = authenticate(username=username, password=password)
login(request, user)
return activated
2)我想为用户添加一个选项,以便能够单击按钮以接收另一个激活电子邮件(如果他未能收到第一个)。以下是注册时发送激活电子邮件的地方 -
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
如果已创建用户帐户,我将如何发送另一封激活电子邮件?
答案 0 :(得分:14)
1)。
from django.contrib.auth import login
from registration import signals
def login_on_activation(user, request, **kwargs):
user.backend='django.contrib.auth.backends.ModelBackend'
login(request, user)
signals.user_activated.connect(login_on_activation)
2)。 registration.models.RegistrationProfile.send_activation_email
方法。