選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

43 行
1.3 KiB

  1. from django import forms
  2. from django.utils.translation import ugettext_lazy as _
  3. from django.contrib.auth.models import User
  4. class UserCreationForm(forms.ModelForm):
  5. """
  6. A form that creates a user, with no privileges, from the given username,
  7. email, and password.
  8. """
  9. error_messages = {
  10. 'duplicate_username': _("A user with that username already exists."),
  11. }
  12. username = forms.RegexField(
  13. label=_("Username"), max_length=30,
  14. regex=r'^[\w\-.]+$'
  15. )
  16. password = forms.CharField(
  17. label=_("Password"),
  18. widget=forms.PasswordInput
  19. )
  20. class Meta:
  21. model = User
  22. fields = ("username", "email")
  23. def clean_username(self):
  24. # Since User.username is unique, this check is redundant,
  25. # but it sets a nicer error message than the ORM. See #13147.
  26. username = self.cleaned_data["username"]
  27. try:
  28. User._default_manager.get(username=username)
  29. except User.DoesNotExist:
  30. return username
  31. raise forms.ValidationError(self.error_messages['duplicate_username'])
  32. def save(self, commit=True):
  33. user = super(UserCreationForm, self).save(commit=False)
  34. user.set_password(self.cleaned_data["password"])
  35. if commit:
  36. user.save()
  37. return user