djangalauth将注册限制为电子邮件列表

2024-03-29 14:13:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Django allauth,我有一个电子邮件列表,我想限制注册到这个列表。我的想法是检查注册用户的电子邮件,如果不在电子邮件列表中,停止注册过程并重定向。 根据车坛甘地的建议,我尝试了编辑allauth.account.views.SignupView,但它不订阅form\u valid方法。我怎么能做到呢?谢谢你的帮助

from allauth.account.views import SignupView

class AllauthCustomSignupView(SignupView):

    def form_valid(self, form):
        email = form.cleaned_data['email']
        auth_user_list =    [   'email_1',
                                'email_2',
                                ...
                            ]

        if not any(email in s for s in auth_user_list):
            return reverse('url')
        return super(MySignupView, self).form_valid(form)

Tags: inselfformauth列表return电子邮件email
2条回答

您可以通过扩展DefaultAccountAdapter类来实现。你必须想出一种方法来按需存储和获取受限列表。 然后,您可以使用适配器并在注册中从中引发验证错误。扩展DefaultAccountAdapter并重写clean\u email方法。创建适配器.py并扩展默认适配器类。在

from allauth.account.adapter import DefaultAccountAdapter
from django.forms import ValidationError

class RestrictEmailAdapter(DefaultAccountAdapter):

    def clean_email(self,email):
        RestrictedList = ['Your restricted list goes here.']
        if email in RestrictedList
            raise ValidationError('You are restricted from registering. Please contact admin.')
        return email

最后,将帐户适配器指向设置.py给你的扩展班。在

^{pr2}$

也许试试这个代码

class AllauthCustomSignupView(SignupView):

    def form_valid(self, form):
        email = form.cleaned_data['email']
        auth_user_list =    [   'email_1',
                                'email_2',
                                ...
                            ]

        if email in auth_user_list:
            return reverse('blocked-email') # whatever url, make sure that the url is defined inside form_valid or in approriate location.
        else:
            return super(AllauthCustomSignupView, self).form_valid(form)

class BlockedEmailView(TemplateView):
    template_name = "blocked-email.html"

在您的网址.py在

^{pr2}$

此外,还需要更改SignupView所具有的表单的action属性。因此,您必须重写该视图的模板,保持其他所有内容不变,只需将操作更改为指向“signup/”。在

相关问题 更多 >