[英]Best way to determine a new site user - Django
I want a way of checking if someone has filled out their profile information (new user) with django. 我想要一种检查是否有人用django填写了个人资料信息(新用户)的方法。 If they havn't I want to show a modal that won't go away until all information has been filled out.
如果他们没有,我想展示一个模态,直到所有信息都填写完毕,模态才会消失。 No matter what page they go to, it should show this modal until filled out.
无论他们进入哪个页面,它都应显示此模式,直到填写完毕。
Should I do the check with javascript(ajax) to a route that would make the check and return a json request with the answer? 我是否应该使用javascript(ajax)进行路由检查,以进行检查并返回带有答案的json请求? If the json object says they are new, I would append the modal to the screen dynamically.
如果json对象说它们是新的,我将动态地将模式附加到屏幕上。
Update: I use django's authentication system. 更新:我使用django的身份验证系统。 Here is an example of logging in. The check would be similar but I would be using model I made in another app that extends Django's base user class.
这是一个登录示例。检查将类似,但我将使用在另一个应用程序中创建的模型,该应用程序扩展了Django的基本用户类。 I call it user_profile.
我称它为user_profile。 I would probably check to see if the user's first name is set.
我可能会检查一下是否设置了用户的名字。 If it isn't I would wanna perform the check.
如果不是,我想执行检查。
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")
One option is to put a model method on your user_profile
's model: 一种选择是将模型方法放在
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
Then you could check in your view like so: 然后,您可以像这样检查视图:
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..........
The best way would be to create a custom context processor that would check the current user's registered data and set a boolean value in the context
that would be accessible in every single template and view. 最好的方法是创建一个自定义上下文处理器,该处理器将检查当前用户的注册数据并在可以在每个模板和视图中访问的
context
中设置一个布尔值。
It would avoid having to call the code over and over again on all views. 这样可以避免在所有视图上一遍又一遍地调用代码。
You can read about context processors here: https://docs.djangoproject.com/en/1.10/ref/templates/api/ 您可以在此处阅读有关上下文处理器的信息: https : //docs.djangoproject.com/en/1.10/ref/templates/api/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.