0

我想知道解决这个问题的最佳方法。我使用 Userena 作为我的项目的基础。我想将团队花名册加载到数据库或保留花名册的文本文件,在用户注册之前,网站应检查用户是否在花名册上。如果没有,那么他们将无法注册。

4

1 回答 1

0

在 userena.forms 中是 SignupForm。我将扩展表单验证中正在实施的三种干净方法之一。它们是 clean_username、clean_email 和 clean。

例如,下面是 clean_email 方法。它已经检查电子邮件是否已在使用中。我会维护一个包含有效电子邮件的名册表。因此,您可以添加另一层检查。我会把它放在第一个下面。

def clean_email(self):
    """ Validate that the e-mail address is unique. """
    if User.objects.filter(email__iexact=self.cleaned_data['email']):
        raise forms.ValidationError(_('This email is already in use. Please supply a different email.'))
    return self.cleaned_data['email']

与检查电子邮件是否被其他用户使用相比。对于名册,如果在名册表中找不到它,我们将引发错误。

def clean_email(self):
    """ Validate that the e-mail address is unique. """
    if User.objects.filter(email__iexact=self.cleaned_data['email']):
        raise forms.ValidationError(_('This email is already in use. Please supply a different email.'))
    if not Roster.objects.filter(email__iexact=self.cleaned_data['email']):
        raise forms.ValidationError(_('You are not able to signup as you are not part of the Roster.'))
    return self.cleaned_data['email']

注意:请务必将您的名册模型导入到您添加检查的任何位置。

于 2012-10-31T14:56:20.380 回答