Django forms IntegerField value validation

I create form with IntegerField. Is possibility to validate input values on it, from defined list?

This is a API form, which connect to another DB. It’s not based on model in project.

My form looks like that:

from django.core.exceptions import ValidationError

def validate_users(value):
    users_list = [10012, 16115, 10505]
    if value not in users_list:
        raise ValidationError('Wrong number')

class PribilagesForm(forms.Form):
    mslk_module_choices = (('1', 'one'),
                           ('2', 'two'),)

    workerId = forms.IntegerField(label='Nr. ewidencyjny', validators=[validate_users])
    moduleName = forms.ChoiceField(label='Moduł', choices=mslk_module_choices)

When I input value from out of range validate_users, and submit form, I got ValueError, not information about wrong insert value.

view:

class TestFormPrivilegesView(TemplateView):
    template_name = "SLK_WS_app/SLK/test.html"

    def get(self, request):
        form = PribilagesForm()
        return render(request, self.template_name, {'form':form})

    def post(self,request, **kwargs):
        form = PribilagesForm(request.POST)
        if form.is_valid():
            workerId = form.cleaned_data['workerId']
            moduleName = form.cleaned_data['moduleName']

        # args = {'form': form, 'workerId':workerId, 'moduleName': moduleName, }
            request_data = {'workerId': workerId,
                            'moduleName': moduleName}

            context = super().get_context_data(**kwargs)

            client = zeep.Client(wsdl=ws_source_slk_web_service)
            find_privileges = client.service.FindUserModulePermission(**request_data)

            data_find_privileges = find_privileges['usersModulesPermissions']['UserModulePermissionData']

            privileges_list = []
            privileges_data = namedtuple('FindUserModulePermission', ['workerId',
                                                                      'workerFullName',
                                                                      'moduleName',
                                                                      'modifyDate',
                                                                      'deleted',
                                                                      ]
                                         )

            for element in data_find_privileges:
                privileges_list.append(privileges_data(element['workerId'],
                                                       element['workerFullName'],
                                                       element['moduleName'],
                                                       element['modifyDate'],
                                                       element['deleted'],
                                                       )
                                       )

            context['privileges_list'] = privileges_list

            context['Date_time'] = datetime.datetime.today()

            context['form'] = PribilagesForm()

            return render(request, self.template_name, context)

        render(request, self.template_name, context={'form': PribilagesForm()})```

Answer

At the bottom of your view inside the post method you have:

render(request, self.template_name, context={'form': PribilagesForm()})

This needs to have return like so:

return render(request, self.template_name, context={'form': PribilagesForm()})