在Django中创建用户时执行某些操作
问题描述:
我使用social-auth-app-django与Facebook和Google进行社交身份验证。我尝试创建一个接收器,一旦创建用户来执行某些命令,例如在用户创建后显示教程(仅一次)。在Django中创建用户时执行某些操作
下面是我想要做的,但一直没有得到它的工作。
from social_core.pipeline.user import create_user
@receiver(create_user)
def after_user_created(request, user, **kwargs):
userprofile = UserProfile.objects.create(user=user)
userprofile.full_name = user.first_name + " " + user.last_name
userprofile.save()
request.session['first_login'] = True
return render_to_response("main/highlights/highlights.html",locals(),RequestContext(request))
从本质上讲,我怎么能建立某种形式的接收器的激活一次我创建一个唯一的用户,这样一旦用户已经创建我可以进行自定义操作?我目前的代码似乎没有工作。
答
在你的应用程序的models.py写预先保存的信号,如下图所示:
@receiver(pre_save, sender=User)
def show_tutorial(sender, instance=None, **kwargs):
# For first time creation of user display tutorial
if instance._state.adding:
# display tutorial()
我试图做你所说的话,并把一个的request.session [“first_login”] = TRUE,在那里你把#显示教程(),但我得到这个错误:缺少1所需的位置参数:'请求' – Viji123