【问题标题】:Best way to determine a new site user - Django确定新站点用户的最佳方法 - Django
【发布时间】: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


【解决方案1】:

一种选择是在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..........

【讨论】:

  • 我不认为这是一个非常好的或优雅的解决方案,特别是因为提出问题的人希望在整个网站的每个页面上都有这个。您提供的这个解决方案必须在所有页面上实施。
  • @MarcusLind 请参阅下面的回复 - 如果用户不提供所需数据,您就不会登录,因此这是您唯一需要调用代码的地方。
【解决方案2】:

最好的方法是创建一个自定义上下文处理器,它会检查当前用户的注册数据并在context 中设置一个布尔值,每个模板和视图都可以访问该值。

这样可以避免在所有视图上一遍又一遍地调用代码。

您可以在此处阅读有关上下文处理器的信息: https://docs.djangoproject.com/en/1.10/ref/templates/api/

【讨论】:

  • 我喜欢你的想法,但这只是意味着在每个模板中一遍又一遍地检查布尔值。身份验证系统意味着只有一个地方可以调用代码 - 当用户尝试登录时。如果他们没有通过测试,他们就不会登录;他们必须提供更多数据。我认为实现您正在寻找的另一种方法是自定义中间件,但它的缺点是您必须指定任何不需要需要保护的页面。
猜你喜欢
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
相关资源
最近更新 更多