class ProfileCreateForm(ProfileForm): username = forms.CharField( max_length=30, error_messages={'required': _l(u'A username is required.')}) agreement = forms.BooleanField( required=True, error_messages={ 'required': _l(u'You must agree to the privacy policy to register.') }) class Meta(ProfileForm.Meta): fields = ('name', 'username', 'website', 'bio') def clean_username(self): """Check that the ``username`` hasn't been taken or is invalid""" error_message = _l('This username is not available') username = self.cleaned_data.get('username') if re.match('^[-\w]+$', username) is None: raise forms.ValidationError('Make sure you enter an alphanumeric' ' username') if not username: return username # black list of usernames if username in settings.INVALID_USERNAMES: raise forms.ValidationError(error_message) # user kept his username if self.instance.user.username == username: return username try: User.objects.get(username=username) raise forms.ValidationError(error_message) except User.DoesNotExist: pass return username
class ProfileForm(forms.ModelForm): name = forms.CharField( error_messages={'required': _l(u'A Display Name is required.')}) class Meta: model = Profile fields = ('name', 'website', 'bio')
class ProfileForm(forms.ModelForm): avatar = forms.ImageField(widget=ImageFileInput(), required=False) name = forms.CharField( required=True, error_messages={'required': _l(u'A Display Name is required.')}) class Meta: model = Profile fields = ('name', 'website', 'avatar', 'bio')
class ProfileCreateForm(ProfileForm): agreement = forms.BooleanField( required=True, error_messages={ 'required': _l(u'You must agree to the privacy policy to register.') }) def __init__(self, *args, **kwargs): super(ProfileCreateForm, self).__init__(*args, **kwargs) self.fields['agreement'].label = jinja2.Markup( _(u"I'm okay with Mozilla handling this info as you explain in your " u"<a href='{url}' target='_blank'>privacy policy</a>.")).format( url='http://www.mozilla.org/en-US/privacy-policy.html')
def clean_username(self): """Check that the ``username`` hasn't been taken or is invalid""" error_message = _l('This username is not available') username = self.cleaned_data.get('username') if not username: return username # black list of usernames if username in settings.INVALID_USERNAMES: raise forms.ValidationError(error_message) # user kept his username if self.instance.user.username == username: return username try: User.objects.get(username=username) raise forms.ValidationError(error_message) except User.DoesNotExist: pass return username