繁体   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