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)