0

I'm using a 3rd part app(django-registration), and need to modify the validation rules for my own favor.

One way is to extend the form(RegistrationForm), and override the fields with my own validation rules.

#registration/forms.py
class RegistrationForm(forms.Form):
    username = forms.RegexField(regex=r'^[\w.@+-]+$',
                                max_length=30,
                                ...
                                )
    ...

In my own apps:

class RegistrationForm(RegistrationForm):
    username = forms.RegexField(regex=r'(?ui)^[\w.@+-]+$',
                                min_length=3,
                                max_length=20,
                                ...
                                )
    ...

But it's always a problem where I should put this extended form in my own code.

Another idea is to embed the registration app in my own project folder, and modify it directly. It's easier to do, but feels like a problem regarding OOP.

Wondering what's the best way of doing it?

Xun Yang
  • 4,209
  • 8
  • 39
  • 68

1 Answers1

0

django-registration in particular provides quite a bit of extensibility. The register view allows a form_class parameter

You can override its default url for the register view and set form_class to whatever form class you want that does the validation the way you need it (I suggest subclassing RegistrationForm like you do in your sample code).

Something like this (in your main urls.py):

url(r'^accounts/register/$', 'registration.views.register', {
     'backend': 'registration.backends.default.DefaultBackend',
     'form_class': MyRegistrationForm}, name='registration_register'),
(r'^accounts/', include('registration.backends.default.urls')),
Gonzalo
  • 4,145
  • 2
  • 29
  • 27
  • Sorry for replying late @Gonzalo. Thanks for the info! Actually I'm more concerned if there's a good place to put the `MyRegisterationForm ` definition, should I create an app called "custom_registration" that's only used for subclassing things from registration, or should I put it in `forms.py` in the project root? As I'm encountering a lot of tinkering issues, is there a best practice for this kind of things? – Xun Yang Oct 04 '12 at 08:11
  • 1
    I don't think there's a convention for that. If it's just the form definition, I'd put it under a "customizations" directory within my project, in a forms.py file. Putting it in a forms.py file within the project root is also good, and makes a lot of sense if using Django 1.4. – Gonzalo Oct 04 '12 at 12:50