Django-registration clears all fields if any of them invalid
I've created a registration system using django-registration. I have extended the RegistrationFormTermsOfService as shown below. It all works fine except if any field is invalid the page is refreshed and all the fields are cleared so the user has to enter everything again. Is this the default behaviour for django-registration? I don't really want to change the django-registration code. Is there anyway to get this working so it leaves the fields populated with what the user has entered so far?
class UserCreationFormExtended(RegistrationFormTermsOfServi开发者_如何转开发ce):
email = forms.EmailField(label=("Email"), help_text = "We'll send...")
username = forms.RegexField(
label='Username:',
max_length=35,
min_length=6,
regex=r'^[\w-]{6,35}$',
help_text = 'Between 6 and 35 characters',
error_message = 'Enter between 6 and 35 alphanumeric characters.')
password1 = forms.RegexField(
max_length=30,
min_length=6,
regex=r'^[\w-]{6,35}$',
help_text = 'Between 6 and 35 characters',
error_message = 'Enter between 6 and 35 characters.')
def __init__(self, *args, **kwargs):
super(UserCreationFormExtended, self).__init__(*args,
**kwargs)
self.fields['email'].required = True
class Meta:
model = User
fields = ('username', 'email')
Django-registration does not "flush" the form, if input is not valid the view returns the form filled with the posted data.
Are you passing your form to register view ?
Do you have something like that in your url.py ?
url(r'^register/$', 'registration.views.register', {'form_class':UserCreationFormExtended}),
The Meta class you defined it seems useless to me, do you really need that ? The init also is pretty useless since you can have the same result with this:
email = forms.EmailField(label=("Email"), required= True, help_text = "We'll send...")
but with less code.
Hope it helps :)
精彩评论