如何强制用户在 Django 中完成他们的个人资料

How to force users to complete their profile in Django

我设计了一个学习管理系统,客户可以参加课程、购买书籍和...。 在这个系统中,当用户想要注册课程时,会强制填写他们的个人资料信息。但是他们中的一些人不想注册课程,所以我无法获得他们的信息。

我想强制用户完成他们的个人资料并在他们的个人资料中输入所需的信息。例如我想要:

System show them warning and force them to complete their profile three days after registration.

我该怎么做? 注意:另一方面,我想阻止尚未完成个人资料的用户访问面板,直到他们完成个人资料。

您将需要实现一个中间件。在此 middleware 中,您将获取您的用户并检查他们是否有个人资料,如果没有,则将他们重定向到个人资料页面,例如使用消息。

你的中间件看起来像这样

def check_userprofile_middleware(get_response):
    # One-time configuration and initialization.

    def middleware(request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.

        If not hasattr(request.user, "profile"):
             redirect("complete-profile-page")

        response = get_response(request)

        return response

    return middleware

然后您需要将其添加到 settings.py 中的中间件部分以激活中间件。

希望对您有所帮助!