V
V
Vitaly Ananiev2022-01-12 21:22:27
Django
Vitaly Ananiev, 2022-01-12 21:22:27

What code should be written to display an error, this is not a number, my version does not work?

forms.py

def clean_price(self):
    price = self.cleaned_data['price']
    if price < 1:
        raise ValidationError('Цена не может быть ниже 1')
    elif not isinstance(price, int):
        raise ValidationError('Это не число!')
    return price

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fedor, 2022-01-12
@wtfedor

if isinstance(price, int) is False:
    raise ValidationError('Это не число!')
if price < 1:
    raise ValidationError('Цена не может быть ниже 1')
return price

You must first check whether the price is a number, and then compare it with something.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question