簡體   English   中英

如果條件在 django 后請求中不起作用

[英]if condition is not working in django post request

我正在檢查請求中的 null 值,但在將條件放入視圖后在 null 上創建記錄,您可以檢查我的代碼。

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()

我發送的text2text3 null 但在數據庫中創建我不明白為什么要創建它們。 謝謝

我可能傾向於使用if len(text2) > 0:或 just if text2 您可能會在 POST 數據中得到空字符串"" ,因此您的測試始終為True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM