如何设置django-registration需要手动激活账户?

发布于 2024-10-07 05:20:05 字数 162 浏览 0 评论 0原文

如何设置 django-registration 需要管理员手动激活帐户?

帐户持有者单击电子邮件中的链接后,我希望向管理员发送一封电子邮件,并且他还需要单击一个链接,然后帐户才会激活。

django-registration 是否可以,或者我需要使用其他东西,以及使用什么?

How to set-up django-registration that an account needs to be manually activated by an admin?

After the account-holder clicks in the link in the e-mail I would like that an e-mail is sent to the admin, and he also needs to click a link and only then the account would be active.

Is it possible with django-registration, or do I need to use something else, and what to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹 2024-10-14 05:20:05

django-registration 允许您编写自定义后端来处理自定义激活需求。

因此,您要做的就是创建自己的后端,自己实现注册激活

下面是如何实现 register 函数的示例:

def register(self, request, **kwargs):
    username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
    if Site._meta.installed:
        site = Site.objects.get_current()
    else:
        site = RequestSite(request)
    new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                                password, site, 
                                                                send_email=False)
    # send an email to the admins with user information
    send_new_user_notification(new_user) # you would write this function
    signals.user_registered.send(sender=self.__class__,
                                 user=new_user,
                                 request=request)
    return new_user

关键点是确保 send_email 设置为 false;这将阻止用户获得激活链接。然后,您可以决定发送给管理员的电子邮件是否包含激活链接,或者您是否同意他们转到管理员并仅选中“活动”框。

如果您使用 django.contrib.auth 中的 AuthenticationForm 那么它会自动拒绝 is_active 为 False 的用户,但如果您不使用它然后确保对需要活动用户的任何请求运行以下检查:

def restricted_view(request):
    if request.user and request.user.is_active:
        #continue with the code

您还可以编写自己的装饰器(查看@login_required以获取指针)。请注意,@login_required 不会检查 is_active

django-registration allows you to write a custom backend to handle custom activation needs.

So what you'd do is create your own backend, implementing the register and activate yourself.

Here's an example of how you could implement the register function:

def register(self, request, **kwargs):
    username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
    if Site._meta.installed:
        site = Site.objects.get_current()
    else:
        site = RequestSite(request)
    new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                                password, site, 
                                                                send_email=False)
    # send an email to the admins with user information
    send_new_user_notification(new_user) # you would write this function
    signals.user_registered.send(sender=self.__class__,
                                 user=new_user,
                                 request=request)
    return new_user

The key point is to make sure send_email is set to false; that will prevent the user from getting an activation link. Then you can decide whether the email sent to the admins has an activation link or if you're content with them going to admin and just checking the "Active" box.

If you use AuthenticationForm from django.contrib.auth then it will automatically reject users whose is_active is False, but if you're not using that then make sure to run the following check for any request where an active user is required:

def restricted_view(request):
    if request.user and request.user.is_active:
        #continue with the code

You can also write your own decorator (look at @login_required for pointers). Note that @login_required does not check for is_active.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文