I have looked at a lot of posts that had similar issues and the answers did nothing for my code.
I am using a custom registration form for a site I'm working on that makes email required. most of the code in it is based off of the one built in to django. I have tried to create a new form class based on django.contrib.auth.forms.UserCreationForm
and still get the same error.
The form based on the one in django
class UserCreateFrom(UserCreationForm):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
error_messages = {
'duplicate_username': _("A user with that username already exists."),
'duplicate_email': _("That email is already in use"),
'password_mismatch': _("The two password fields didn't match."),
}
email = forms.EmailField(label=_("Email Address"),
required=True,
help_text="Required. Your Email Address for password resets, and other emails from us")
def clean_email(self):
email = self.cleaned_data["email"]
try:
User._default_manager.get(email=email)
except User.DoesNotExist:
return email
raise forms.ValidationError(
self.error_messages['duplicate_email'],
code='duplicate_email',
)
the form currently in use
class UserCreateFrom(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
error_messages = {
'duplicate_username': _("A user with that username already exists."),
'duplicate_email': _("That email is already in use"),
'password_mismatch': _("The two password fields didn't match."),
}
username = forms.RegexField(label=_("Username"), 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.")
}
)
email = forms.EmailField(label=_("Email Address"),
required=True,
help_text="Required. Your Email Address for password resets, and other emails from us")
password1 = forms.CharField(label=_("Password"),
widget=forms.PasswordInput)
password2 = forms.CharField(label=_("Password confirmation"),
widget=forms.PasswordInput,
help_text=_("Enter the same password as above, for verification."))
class Meta:
model = User
fields = ("username",)
def clean_username(self):
# Since User.username is unique, this check is redundant,
# but it sets a nicer error message than the ORM. See #13147.
username = self.cleaned_data["username"]
try:
User._default_manager.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(
self.error_messages['duplicate_username'],
code='duplicate_username',
)
def clean_email(self):
email = self.cleaned_data["email"]
try:
User._default_manager.get(email=email)
except User.DoesNotExist:
return email
raise forms.ValidationError(
self.error_messages['duplicate_email'],
code='duplicate_email',
)
def clean_password2(self):
password1 = self.cleaned_data["password1"]
password2 = self.cleaned_data["password2"]
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
return password2
def save(self, commit=True):
user = super(UserCreateFrom, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
the view, I have not been able to work on if the form is valid since I cant get it to work with no data UserCreateFrom
is my custom registration form imported from froms.py in the same app.
@sensitive_post_parameters()
@csrf_protect
@never_cache
def register(request):
"""
Allow registration of new users
:param request:
:return:
"""
context = {}
form = UserCreateFrom(data=request.post)
context.update({'form': form})
if request.method == 'POST' and form.is_valid():
return render(request, 'test.html', context)
else:
return render(request, 'register.html', context)
if I switch form = UserCreateFrom(data=request.post)
to form = UserCreateFrom(request)
I get 'WSGIRequest' object has no attribute 'get' error, but this way at least errors in the template right where the form is called via `{{ form }}, and not the view.
The data attribute is called request.POST
.
But you should move that into the block that checks the method.