【问题标题】:How to set-up django-registration that an account needs to be manually activated?如何设置需要手动激活帐户的 django-registration?
【发布时间】:2010-12-11 02:31:58
【问题描述】:

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

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

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

【问题讨论】:

    标签: django registration


    【解决方案1】:

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

    所以你要做的是创建自己的后端,自己实现registeractivate

    以下是如何实现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.authAuthenticationForm,那么它将自动拒绝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

    【讨论】:

      猜你喜欢
      • 2016-08-01
      • 2013-01-12
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      相关资源
      最近更新 更多