failing the captcha no longer returns a 500

This commit is contained in:
Alie 2023-07-14 20:29:07 +02:00
parent 8631a0259e
commit 3eb3dfca48
2 changed files with 52 additions and 37 deletions

View File

@ -1,3 +1,4 @@
from re import Pattern
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as txt
@ -11,5 +12,5 @@ class SignbookForm(forms.Form):
def validate_capcha(self):
if self.cleaned_data["capcha"].lower() != "i love me":
raise ValidationError(txt('Invalid captcha'))
raise ValidationError(txt('Invalid captcha'), code="invalid")
return self

View File

@ -1,3 +1,4 @@
from django.forms import ValidationError
from django.http import HttpResponseNotFound, HttpResponseRedirect
from django.shortcuts import render
import os, random
@ -47,6 +48,7 @@ def signbook(request):
signer_list = Booksigner.objects.all()
if request.method == 'POST':
form = SignbookForm(request.POST)
try:
if form.is_valid() and form.validate_capcha():
newsigner = {}
newsigner["name"] = form.cleaned_data['name']
@ -87,6 +89,18 @@ def signbook(request):
"signbook.html",
context
)
except ValidationError as e:
print("Error: ", e)
context = {
"title": "THE GUESTBOOK",
"signer_list": signer_list,
"error_message": "Invalid captcha.",
}
return render(
request,
"signbook.html",
context
)
else:
context = {
"title": "THE GUESTBOOK",