Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

5 anni fa
5 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142
  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