gpt4 book ai didi

javascript - 确定新站点用户的最佳方法 - Django

转载 作者:行者123 更新时间:2023-11-29 19:07:52 24 4
gpt4 key购买 nike

我想要一种方法来检查是否有人使用 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")

最佳答案

一种选择是将模型方法放在您的user_profile 的模型上:

class UserProfile(models.Model):
name = CharField...
...other fields...

def get_is_new(self):
if self.name is None: # You could include other checks as well
return True
return False

然后你可以像这样检查你的 View :

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")
if user.get_is_new() is True:
# Return the modal
request.session['id'] = user.id
.......rest of your code..........

关于javascript - 确定新站点用户的最佳方法 - Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41410276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com