Python, how to validate if the variable is None?

1

I'm not able to check if the variable has no value (None)

View:

endereco_id = request.POST.get('endereco_id', None)
if formCliente.is_valid() and formEndereco.is_valid():
    print('********** endereco_id **************')
    print(endereco_id)
    print('************************')
    if endereco_id:
       formEndereco.instance= Endereco.objects.get(id=endereco_id)

O print:

********** endereco_id **************
None
************************

Why is entering the IF if in print the value of the variable is None?

Attempts:

if endereco_id != None:
     formEndereco.instance= Endereco.objects.get(id=endereco_id)

if endereco_id is not None: 
     formEndereco.instance= Endereco.objects.get(id=endereco_id)

Nothing worked, where am I going wrong? The Python version is 3.5 with Django 1.10.

No Shell works:

>>> endereco_id=None
>>> if endereco_id:
...     print('Possui valor')
... else:
...     print('É None')
... 
É None
>>> endereco_id=1
>>> if endereco_id:
...     print('Possui valor')
... else:
...     print('É None')
... 
Possui valor
>>> 

View:

def novo_cliente(request):

    formCliente = ClienteForm(request.POST or None)
    formEndereco = EnderecoForm(request.POST or None)


    if request.method == 'POST':
        cliente_id = request.POST.get('cliente_id', None)
        endereco_id = request.POST.get('endereco_id', None)

        if formCliente.is_valid() and formEndereco.is_valid():
            if endereco_id is not None:
                formEndereco.instance= Endereco.objects.get(id=endereco_id)

            if cliente_id is not None:
                formCliente.instance= Cliente.objects.get(id=cliente_id)

            novo_endereco = formEndereco.save()
            novo_cliente = formCliente.save(commit=False)
            novo_cliente.endereco = novo_endereco
            novo_cliente.save()

            return redirect('appOrcamento:edit_cliente', orcamento.id)


    context = {
        'formCliente':formCliente,
        'formEndereco':formEndereco,
        }

    return render(request, 'appOrcamento/novo_cliente.html', context)
    
asked by anonymous 14.04.2017 / 19:10

1 answer

3

With the help of @Edilson, we were able to identify the problem. It's basic, but for those of you starting in Python with Django, it can become tricky. For this reason I will explain what happened.

When retrieving the value of a field in the POST in the view, if the field does not exist in the template the return is None, that is, there is no field, it would be null. When the field exists in the template, but if there is no information the return at the post is a str 'None'.

This was my problem, in the print was printing None, but it was of type str and the if did not work correctly.

Ex:

client_id = request.POST.get ('client_id', None)

If client_id does not exist in the template, the return will be None (null)

If client_id exists in the template, but no value, the return will be a 'None' str

The solution:

def novo_cliente(request):

    formCliente = ClienteForm(request.POST or None)
    formEndereco = EnderecoForm(request.POST or None)


    if request.method == 'POST':
        cliente_id = request.POST.get('cliente_id', None)
        endereco_id = request.POST.get('endereco_id', None)

        if formCliente.is_valid() and formEndereco.is_valid():
            if endereco_id != 'None':
                formEndereco.instance= Endereco.objects.get(id=endereco_id)

            if cliente_id != 'None':
                formCliente.instance= Cliente.objects.get(id=cliente_id)

            novo_endereco = formEndereco.save()
            novo_cliente = formCliente.save(commit=False)
            novo_cliente.endereco = novo_endereco
            novo_cliente.save()

            return redirect('appOrcamento:edit_cliente', orcamento.id)


    context = {
        'formCliente':formCliente,
        'formEndereco':formEndereco,
        }

    return render(request, 'appOrcamento/novo_cliente.html', context)

I hope this answer can help beginners.

@Edilson, thanks for the help.

    
15.04.2017 / 01:59