views:

94

answers:

1

Django 1.2 adds new allowable symbols to usernames, meaning usernames can simply be email addresses. Up until now I have been using the inbuilt UserCreationForm for registration - how can I alter it to label the 'username' field the 'email' field? And how also to add extra (but still User object) fields such as first and last names? (And how to make them optional?)

Should I be altering the UserCreationForm to this extent or am I better starting from scratch (and if the latter, how?)

Thanks.

+1  A: 

To change the label of the email field, you can subclass the UserCreationForm as follows

from django.contrib.auth.forms import UserCreationForm

class MyUserCreationForm(UserCreationForm):
    username = forms.RegexField(label=_("Email"), max_length=30, regex=r'^[\w.@+-]+$',
        help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."),
        error_messages = {'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})

Adding first_name and last_name fields is not quite so easy, because the form's save method only takes username, email and password. Two possibilities are:

  • override the form's save method
  • display a UserChangeForm once the User has been created (this is what the Django admin app does)
Alasdair