Home > Mobile >  if condition is not working in django post request
if condition is not working in django post request

Time:02-08

I'm check null values from the request but creating record on null after put the condition in views you can check my code.

if request.method == 'POST':
    category_id = request.POST['category_id ']
    text1       = request.POST['text1']
    text2       = request.POST['text2']
    text3       = request.POST['text3']

    product = Product(category_id=category_id, text=text1) 
    product.save()

    if text2 is not None:
        product = Product(category_id=category_id, text=text2) 
        product.save()
        
    if text3 is not None:
        product = Product(category_id=category_id, text=text3) 
        product.save()

The text2 and text3 I'm sending null but creating in the database I'm not understanding why these are creating. Thanks

CodePudding user response:

I might be inclined to use if len(text2) > 0: or just if text2. You might be getting empty strings "" in the POST data so your tests are always True.

  •  Tags:  
  • Related