I’m currently implementing a user registration for an app. I’m tying to change the django.auth default behaviour to use email instead of username. I think i’ll use a custom auth backend. I want the users still be able to provide an username.
-
How can I allow spaces in the username like “john doe” without changing the fields in django.auth models.
-
How do I make the emailfield required. currently the clean_email method in my UserCreation Form raises an validation error if email is empty. There must be a better way.
Thanks to Brandon and Andrew Sledge here is my solution:
class RegisterForm(UserCreationForm):
username = forms.RegexField(label=("Username"), max_length=30, regex=r'^[ \t\r\n\f\w.@+*-]+$',
help_text = ("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."),
error_messages = {'invalid': ("This value may contain only letters, numbers and @/./+/-/_ characters.")})
class Meta:
model = User
i copied the field from UserCreationForm. The important part is to add \t\r\n\f to the regex. \s wont work.
You can always override the UserCreationForm from contrib.auth.forms to implement a different field for username and provide your own validation. Just inherit the form and add your own username field, or override it in the
__init__of the class.