master_j02
master_j02

Reputation: 405

Django form.is_valid() is False, but not hitting the else statement?

I am working on something that should be pretty simple, but running into a very odd form issue.

@csrf_protect
def employee_create(request, company_id):

    template_name = 'business/employee-create.html'
    data = dict()
    company = Company.objects.get(id=company_id)


    if request.method == 'POST':
        employee_form = EmployeeForm(request.POST)
        if employee_form.is_valid():
            logger.debug('Valid')
            emp = employee_form.save(commit=False)
            emp.company = venue
            emp.user = request.user
            emp.save()
            return redirect('comp_detail', company_id=company_id)
        else:
            logger.debug('Error')

    else:
        employee_form = EmployeeForm()
        data['html_form'] = render_to_string(
            template_name,
            {'form': employee_form, 'company_id': company.id},
            request=request,
        )
    return JsonResponse(data)

The GET request is just a popup bootstrap form. But my odd situation is happening in the POST request. I am sending an invalid form that is completely empty(in the form modal I get djangos built in form errors popping up) , but the else statement never gets hit, that is supposed to print Error in my django logger.

The form is invalid, but is also not invalid? There has to be something that I am missing here??

Thanks in advance for anyone that takes a glance!

Upvotes: 1

Views: 483

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477180

The logger is usually set to a certain log level. This means that, depending on the level, some logging may not be logged. The log levels [Python-doc] are in ascending order: NOTSET, DEBUG, INFO, WARNING, ERROR and CRITICAL.

The root logger is normally created with WARNING as threshold, so INFO, DEBUG and NOTSET will not be logged.

You can alter the threshold value, or you can log the messages with a higher level:

if employee_form.is_valid():
    logger.error('Valid')
    # …
else:
    logger.error('Error')

Upvotes: 3

Related Questions