简体   繁体   English

如果条件在 django 后请求中不起作用

[英]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.我发送的text2text3 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.

相关问题 request.method == 'POST' 在 Django 中不起作用 - request.method == 'POST' is not working in Django Django Test request.method =='POST'不起作用 - Django Test request.method == 'POST' not working POST 请求方法在 Django Rest Framework 中不起作用 - POST Request Method not working in Django Rest Framework Django REST 通用 ListCreateView GET 请求有效但 POST 请求无效 - Django REST generic ListCreateView GET request is working but POST request is not working POST 请求在使用 Plivo 制作的 Django IVR 中不起作用 - POST request not working in Django IVR made using Plivo Request.POST.get 在 Django 中对我不起作用,返回默认值 - Request.POST.get not working for me in django, returning default value 安装 django 主机后,带有 ajax 的 POST 请求不起作用 - POST request with ajax not working after installing django hosts 为什么我的 NodeJS 发布请求不工作? (来自姜戈) - Why Isn't My NodeJS Post Request Working? (From Django) if request.method == 'POST' and 'sub' in request.form: is not working in flask: if condition is not execution - if request.method == 'POST' and 'sub' in request.form: is not working in flask: if condition is not executing 在Django中处理POST请求 - Processing POST request in Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM