Я пытаюсь создать настраиваемое поле капчи для формы, и кажется, что оно работает нормально, за исключением того факта, что когда код должен выбрать случайную капчу, чтобы вернуться к конечному пользователю для решения , он возвращает ValueError: too many values to unpack (expected 2)
. Я думаю, это потому, что список не рандомизирован, а python выбирает весь список для использования в качестве капчи пользователя. Как я могу исправить эту проблему?
class CaptchaField(IntegerField):
widget = CaptchaInput
error_msgs = { 'incorrect': _('Captcha incorrect- try again'), }
def __init__(self):
captcha_array = (
('What is the product of fifteen and four?', 60),
('What is four plus four?', 8),
('What is nine times one?', 9),
('How many letters are in the word orange?', 6),
('What is the sum of ten and two?', 12),
('What is the difference of eighty-four and nineteen?', 65),
('How many letters are in the word forest?', 6),
('How many letter are in the word apple?', 5),
('If there are four palm trees and one dies, how many are alive?', 3),
('What is four divided by two?', 2),
('How many letters are in the name of the capital of France?', 5),
)
captcha = random.choice(captcha_array)
for (a,b) in captcha:
return a
def validate(self, value):
for (a,b) in captcha:
if value == b:
return value
else:
raise ValidationError(self.error_msgs['incorrect'], code = 'incorrect')