Reputation: 27
EDIT:
I am leaving this up in case anyone has the same stupidity as me.
Solution: Restart the apache server..
Explanation: I was changing things live while it was running, some things I changed were sent right over and worked immediately. Where some needed the server to restart to take effect. I just spent a lot of time changing as many things as possible to try and get it working when all it took was a restart.
END EDIT
I am attempting to make a registration page and I have been getting Keyerrors whenever I try and validate.
I have tried looking for a solution for quite awhile as well as looking at a lot of stack overflow answers which I believe should be working, but have not worked at all for whatever reason.
The error always occurs on the same line, no matter which field is left blank (password1 or password2)
Clean:
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data and self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError("Passwords do not match")
return cleaned_data
Elements of the form:
username = forms.CharField(required = False)
password1 = forms.CharField(required = False, widget=forms.PasswordInput, label='Password')
password2 = forms.CharField(required = False, widget=forms.PasswordInput, label='Repeat Password')
email = forms.EmailField(required = False)
Relevent from views.py:
if request.method == 'POST':
form = UserForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect("/register/success")
else:
form = UserForm()
return render(request, 'register.html', {'form':form})
My template for the form in HTML is a table with 4 rows and 3 columns per row. The first column is the errors which is normally blank but pops up errors related to the field if there are any. The second is the label for the field, and the third is the textbox for the field itself.
Edit: I decided to test it a little bit more before submitting, it appears to happen at line 44, no matter what is at line 44. If it's in a different function? Doesn't matter, line 44. All of the fields were originally required but I changed them all to False during this testing, I do know that if I were to leave username/email blank it would error out at this point.
Here is the stack trace:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/register/views.py" in register
10. if form.is_valid():
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in is_valid
161. return self.is_bound and not self.errors
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in errors
153. self.full_clean()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in full_clean
363. self._clean_form()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _clean_form
390. cleaned_data = self.clean()
File "/register/forms.py" in clean
44. temp = 2
Exception Type: KeyError at /register/
Exception Value: 'password1'
Upvotes: 1
Views: 486
Reputation: 309089
[the error] appears to happen at line 44, no matter what is at line 44. If it's in a different function? Doesn't matter, line 44.
You need to restart your server after changing code. If the server is still saying the error is on line 44, that means that you haven't restarted the server since changing the code.
If you are using the Django developement server (i.e. ./manage.py runserver
) then it should detect code changes and restart. However if you are in production, then you may need to restart your server (e.g. Apache) manually.
Hopefully restarting your server will make the error go away. If not, please update the error with the full form and full traceback.
Upvotes: 2