如何在django-registration应用程序中禁用电子邮件激活?

12
如何在django-registration应用中禁用电子邮件激活?
5个回答

13

7
为什么不使用这种方法,而是使用与django-registration一同提供的简易后端,代替默认的后端呢?
新的工作流程如下: 1. 用户通过填写注册表单进行注册。 2. 用户账户立即被创建并激活,没有中间的确认或激活步骤。 3. 新用户立即登录。
因此,在您的主urls.py中,您需要更改:
url(r'^accounts/', include('registration.backends.default.urls')),

to

url(r'^accounts/', include('registration.backends.simple.urls')),

6

最好在根源处解决问题,而不是通过调用命令自动激活用户来进行包扎。

将此方法添加到注册models.py中:

   def create_active_user(self, username, email, password,
                             site):
        """
        Create a new, active ``User``, generate a
        ``RegistrationProfile`` and email its activation key to the
        ``User``, returning the new ``User``.
        """
        new_user = User.objects.create_user(username, email, password)
        new_user.is_active = True
        new_user.save()

        registration_profile = self.create_profile(new_user)

        return new_user
    create_active_user = transaction.commit_on_success(create_active_user)

接下来,编辑registration/backend/defaults/init.py文件并找到register()方法。

将以下内容更改为调用您的新方法:

#new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                            #password, site)
new_user = RegistrationProfile.objects.create_active_user(username, email,
                                                            password, site)

我按照您的步骤操作,但是当我输入用户时,它没有被激活。 - XMen

3

您可以随时修改这一行代码为:

new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
                                       password=self.cleaned_data['password1'],
                                       email=self.cleaned_data['email'],
                                       profile_callback=profile_callback,
                                       send_email = False)

或者您可以将这一行更改为:

def create_inactive_user(self, username, password, email,
                         send_email=False, profile_callback=None):

2
你需要调用 activate_user 函数(否则用户的账户将无法使用)。 - Jiaaro
1
在django-register的新版本中,文件forms.py到目前为止还不太好用。请查看此链接:http://bitbucket.org/ubernostrum/django-registration/src/tip/registration/forms.py - anhtran
这个很简单,不需要修改代码。哎呀,我没意识到这个答案有多久了... - teewuane
整个答案似乎已经过时了,包括所有链接和逻辑都在不同的位置。 - erikbstack

1

不要修改注册应用程序,为什么不像django-registration一样激活用户:

user.is_active = True user.save() profile.activation_key = "ALREADY_ACTIVATED" profile.save()

经过更深入的研究...我认为你想要使用两种解决方案。可能在Dominic建议的更改之后添加上述代码(尽管我建议使用信号或子类化表单)

好的最终答案:

new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
                                   password=self.cleaned_data['password1'],
                                   email=self.cleaned_data['email'],
                                   profile_callback=profile_callback,
                                   send_email = False)
RegistrationProfile.objects.activate_user(new_user.activation_key)

我搞错了吗?我还没有尝试过我的代码,只是查看了django-registration中的代码。 - Dominic Rodger
不,实际上我刚刚编辑了我的答案,因为我认为我们两个的解决方案都是必要的 :) - Jiaaro
这应该放在哪里?你知道有没有一种方法可以在不修改django-registration代码的情况下完成这个操作吗?我想到了创建用户时发送一个信号的方式。 - teewuane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接