简体   繁体   English

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

[英]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.

相关问题 确定新数据是否可用的最佳方法 - Best way to determine if new data available JS / Jquery方法来确定用户是否从另一个站点进入页面 - JS/Jquery way to determine if the user entered page from another site 确定用户是否实际使用Safari的最佳方法是什么? - What is best way to determine if the user is actually using Safari? 用户离开网站时生成弹出窗口的最佳方法是什么 - What is the best way to generate a popup when user goes to leave site 在重定向到登录之前,阻止Angular站点上的未授权用户暂时查看站点的最佳方法是什么? - Best way to prevent unauthorized user on Angular site from seeing site for a split second before being redirected to login? 在网站上过滤数据的最佳方式 - Best way to filter data on site 确定帧是否是跨域的最佳方法? - Best way to determine if frame is cross-domain? 在 JavaScript 中确定日期是否为今天的最佳方法是什么? - What is the best way to determine if a date is today in JavaScript? 允许用户重新排列Django管理网站中的条目,以及如何存储该新的自定义订单以在页面刷新后保存下来? - Allowing user to rearrange entries in Django admin site and how to store that new custom order to survive page refresh? 是否有JavaScript用户跟踪框架/最佳实践网站? - Is there a JavaScript user tracking framework / best practice site?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM