【发布时间】:2017-02-18 08:02:41
【问题描述】:
我已经设置了一个基本的 Django 站点并添加了登录到该站点。此外,我创建了一个学生(个人资料)模型,该模型扩展了内置用户一。它与用户模型具有 OneToOne 关系。
但是,强制用户在他们第一次登录时自动创建个人资料,我还没有做到正确。我如何确保他们在不创建个人资料的情况下无法完成任何事情?
我尝试在视图中定义以下内容:
def UserCheck(request):
current_user = request.user
# Check for or create a Student; set default account type here
try:
profile = Student.objects.get(user = request.user)
if profile == None:
return redirect('/student/profile/update')
return True
except:
return redirect('/student/profile/update')
然后添加以下内容:
UserCheck(request)
在我的每个视图的顶部。但是,这似乎永远不会重定向用户来创建配置文件。
有没有最好的方法来确保用户在上面创建一个配置文件对象?
【问题讨论】:
标签: django authentication django-models django-views profile