【发布时间】:2016-12-31 17:02:52
【问题描述】:
我想要一种方法来检查是否有人使用 django 填写了他们的个人资料信息(新用户)。如果他们没有,我想展示一个在填写所有信息之前不会消失的模式。无论他们转到哪个页面,它都应该显示此模式,直到填写完毕。
我是否应该使用 javascript(ajax) 对可以进行检查并返回带有答案的 json 请求的路由进行检查?如果 json 对象说它们是新的,我会动态地将模态附加到屏幕上。
更新:我使用 django 的身份验证系统。这是一个登录示例。检查将是类似的,但我将使用我在另一个应用程序中创建的模型,该应用程序扩展了 Django 的基本用户类。我称之为 user_profile。我可能会检查是否设置了用户的名字。如果不是,我想执行检查。
def auth_login(request):
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user:
# the password verified for the user
if user.is_active:
print("User is valid, active and authenticated")
request.session['id'] = user.id
request.session['email'] = user.email
login(request, user)
data = {}
data['status'] = "login"
return HttpResponse(json.dumps(data), content_type="application/json")
#redirect
else:
print("The password is valid, but the account has been disabled!")
else:
# the authentication system was unable to verify the username and password
print("The username and password were incorrect.")
data = {}
data['status'] = "The username and password are incorrect"
return HttpResponse(json.dumps(data), content_type="application/json")
return HttpResponse("hello")
【问题讨论】:
-
有很多方法可以做到这一点。您可以提供更多上下文来缩小答案的范围。你有一个认证系统,即你能检查
request.user.is_authenticated()吗? -
也许提供您的视图代码,因为这是最有可能发生此检查的地方。
-
@YPCrumble 我用一个例子更新了这个问题。是的,我使用 Django 的身份验证系统。
标签: javascript ajax django