2

对 Django 来说相当新,并且有一个关于 Django 注册的问题。

我已经成功启动并使用电子邮件验证正确注册用户,现在我想做的是修改功能,以便只允许具有特定域电子邮件地址的人注册。因此,例如,如果“xyz.org”是域,那么只有尝试使用 blahblahblah@xyz.org 电子邮件注册的用户才会成功。

下面是我到目前为止处理这一切的相关代码,减去注册成功等。我想我基本上只需要能够插入一种新类型的表单对象来验证电子邮件字段,但我不确定如何。非常感谢所有冗长的内容,因为我对此仍然有些粗略。谢谢!

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

`

{% block content %}

{% if form.errors %}
<p>Reg form errors</p>
{% endif %}

<form method="post" action=''>
{% csrf_token %}
{{ form.as_p }}

<input type="submit" value="Send activation email" />
</form>

{% endblock %}
4

1 回答 1

3

You will need to subclass registration.forms.RegistrationForm and add a custom validator for the email field, a RegexValidator will work.

from registration.forms import RegistrationForm

class NewRegistrationForm(RegistrationForm):
    def __init__(self, *args, **kwargs):
        super(NewRegistrationForm, self).__init__(*args, **kwargs)
        self.fields['email'].validators = [validate_domain]

After you have the validator, and the new form, you will have to use this new form for the registration view. In your urls.py:

urlpatterns = patterns(
    ...
    url(r'^register/$', register, { 'form_class': NewRegistrationForm }, name='registration_register')
    ...
)

I haven't tested this out, but I hope it puts you on the right track!

于 2013-01-23T22:04:32.227 回答