Validating string for possible space returns error in Django form even though there’s no space

I have two fields in my Django form where I want the user to insert exactly one word. So to have some validation in the view i created the following validator (idea is to search for spaces):

Views.py

[..]

if ' ' in poller_choice_one or poller_choice_two:
    raise ValidationError(('Limit your choice to one word'), code='invalid')
else:

[..]

To make it more robust I added the strip option to the FormFields to be validated:

    # Poller Choices
    poller_choice_one = forms.CharField(label='Choice one', max_length=20, strip=True)
    poller_choice_two = forms.CharField(label='Choice two', max_length=20, strip=True)

I tried like a bunch of inputs from single digits to single chars etc., it always raises the validation error

Answer

Your if statement is interpreted as:

if (' ' in poller_choice_one) or (poller_choice_two):
    # …

it will first check if there is a space in the poller_choice_one. If that is not the case it will evaluate the truthiness of the poller_choice_two. A string has as truthiness True if it contains at least one character. So the if condition will be True from the moment there is a space in poller_choice_one, or poller_choice_two has at least one character.

You thus should rewrite the condition to:

if ' ' in poller_choice_one or ' ' in poller_choice_two:
    raise ValidationError(('Limit your choice to one word'), code='invalid')
# …