-->

How to make django-registration use my customized

2019-02-20 17:17发布

问题:

I am developing a website using Django 1.4 and django-registration

I would like to allow users to create their user names using arbitrary Unicode characters. Currently, if someone tries to register using non-latin characters, he will see an error message. The code responsible for rejecting this kind of non-ASCII usernames is in UserCreationForm and UserChangeForm, see here:

username = forms.RegexField(
        label=_("Username"), max_length=30, regex=r"^[\w.@+-]+$",
        help_text = _("Required. 30 characters or fewer. Letters, digits and "
                      "@/./+/-/_ only."),
        error_messages = {
            'invalid': _("This value may contain only letters, numbers and "
                         "@/./+/-/_ characters.")})

Now, I would like to change it. I've seen some suggestions that I should create my own UserCreationForm, and creating a derived class with some changed behaviour seems easy enough.

The bit I don't understand is: how to make django-registration use my customized UserCreationForm and UserChangeForm? Obviously, I would like to avoid modifying the source of django-registration, if at all possible.

回答1:

See http://docs.b-list.org/django-registration/0.8/views.html

After creating your own registration form you can pass this form to the register view of django-registration. Look for the registration.backends.default.urls module

url(r'^register/$', register,
     {'backend': 'registration.backends.default.DefaultBackend', 
     'form_class': MyRegistrationForm},