1

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()})```
4
  • 1
    Can you share the full error stack trace? Commented Jun 23, 2021 at 8:59
  • Exception Type: ValueError Exception Value: The view SLK_WS_app.views.TestFormPrivilegesView didn't return an HttpResponse object. It returned None instead. Commented Jun 23, 2021 at 9:19
  • 1
    @PawełDawicki: share your view code, the error is not in the form, but in the view that works with this form. Commented Jun 23, 2021 at 9:20
  • insert value in 'Nr. ewidencyjny' is transferred to API as parametr, but is not validated view i will insert to question, becourse is to long Commented Jun 23, 2021 at 9:25

1 Answer 1

1

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()})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.