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.
Maybe you would like to use validate_slug:
A RegexValidator instance that ensures a value consists of only letters, numbers, underscores or hyphens
For the user name: Override the UserCreationForm (django.contrib.auth.forms) to modify the regex parameter for the field to allow for spaces. No changes to the model are necessary.
For the email: http://djangosnippets.org/snippets/74/
来源:https://stackoverflow.com/questions/9828177/django-auth-manipulate-model-fields-allow-space-in-usernames