[英]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.我正在检查请求中的 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()
The text2
and text3
I'm sending null but creating in the database I'm not understanding why these are creating.我发送的text2
和text3
null 但在数据库中创建我不明白为什么要创建它们。 Thanks谢谢
I might be inclined to use if len(text2) > 0:
or just if text2
.我可能倾向于使用if len(text2) > 0:
或 just if text2
。 You might be getting empty strings ""
in the POST data so your tests are always True
.您可能会在 POST 数据中得到空字符串""
,因此您的测试始终为True
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.