I am having this error when I access a view. Especifically saying that my error is in
`{% if form.errors %}
<p>{{form.errors}}</p>
{% endif %}`"
But this error, is strange to me. I have a form in this view.
class UserCreationLabForm(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.")})
telephone = forms.IntegerField(required=False)
is_secretarial = forms.BooleanField(required=False)
class Meta(UserCreationForm.Meta):
model = CustomUser
fields = ('email', 'user_type', 'telephone', 'FKLab_User', 'is_secretarial',)
exclude = ['user_type','FKLab_User', 'username',]
def clean(self):
cleaned_data = super(CustomUser,self).clean()
email = self.cleaned_data.get('email')
telephone = self.cleaned_data.get('telephone')
is_secretarial = self.cleaned_data.get('is_secretarial')
username = self.cleaned_data.get('username')
if len(email) == 0:
self._errors['email'] = self.error_class(['Insert cant be empty.'])
if len(telephone) == 0:
self._errors['telephone'] = self.error_class(['Insert cant be empty.'])
if len(is_secretarial) == 0:
self._errors['is_secretarial'] = self.error_class(['Insert cant be empty.'])
if len(username) == 0:
self._errors['username'] = self.error_class(['Insert cant be empty.'])
return self.cleaned_data
I had to override it to only accept email. But It was working fine yesterday.
My view
@login_required
@permission_classes((IsAuthenticated,))
#Registration view.
def RegistrationLab_View(request):
fklab_user = HospitalViewRoleForUsers.objects.get(id = request.user.FKLab_User.id)
print(fklab_user.id)
if request.user.is_authenticated and request.method == "POST":
form = UserCreationLabForm(request.POST)
if form.is_valid():
new_user = form.save(commit=False)
#new_user.username =
new_user.FKLab_User = fklab_user
new_user.user_type = 1
new_user.save()
messages.success(request, "The user was successfully created.")
return HttpResponseRedirect('/register/')
else:
return messages.error(request, "Please correct the error below.")
else:
form = UserCreationLabForm(request.user)
return render(request,'user_profile/register/registerlab_user.html', {'form': form})
Anyone knows why this error happens? I have been searching everywhere for answers.
CodePudding user response:
isn't it better that you use render everywhere?
@login_required
@permission_classes((IsAuthenticated,))
#Registration view.
def RegistrationLab_View(request):
fklab_user = HospitalViewRoleForUsers.objects.get(id = request.user.FKLab_User.id)
print(fklab_user.id)
if request.user.is_authenticated and request.method == "POST":
form = UserCreationLabForm(request.POST)
if form.is_valid():
new_user = form.save(commit=False)
#new_user.username =
new_user.FKLab_User = fklab_user
new_user.user_type = 1
new_user.save()
messages.success(request, "The user was successfully created.")
return HttpResponseRedirect('/register/')
else:
//if you want to use the error that the django form returns, in your html you call form.error returning this
return render(request,'user_profile/register/registerlab_user.html', {'form': form})
else:
form = UserCreationLabForm(request.user)
return render(request,'user_profile/register/registerlab_user.html', {'form': form})
and in your html try this
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div >
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% endif %}