我想要一种方法来检查是否有人用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")
答案 0 :(得分:3)
一种选择是在您的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
然后你可以这样查看你的观点:
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..........
答案 1 :(得分:0)
最好的方法是创建一个自定义上下文处理器,它将检查当前用户的注册数据,并在context
中设置一个布尔值,该值可以在每个模板和视图中访问。
这样可以避免在所有视图上反复调用代码。
您可以在此处阅读上下文处理器: https://docs.djangoproject.com/en/1.10/ref/templates/api/