简体   繁体   中英

if condition is not working in django post request

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM