如何在发布数据时检查名称/值对是否存在?

时间:2022-09-29 07:15:42

I'm not able to find the proper syntax for doing what I want to do. I want to do something if a name/value pair is not present. Here is the code in my view:

我不能找到正确的语法来做我想做的事情。如果没有名称/值对,我想做点什么。以下是我认为的代码:

if (!request.POST['number']):
    # do something

What is the proper way to accomplish something like the above? I am getting a syntax error when I try this.

完成上述事情的正确方法是什么?当我尝试这个时,我得到了一个语法错误。

3 个解决方案

#1


32  

@Thomas gave you the generic way, but there is a shortcut for the particular case of getting a default value when a key does not exist.

@Thomas为您提供了通用的方法,但是在不存在密钥时,有一个获取默认值的特殊情况的快捷方式。

number = request.POST.get('number', 0)

This is equivalent to:

这相当于:

if 'number' not in request.POST:
    number = 0
else:
    number = request.POST['number']

#2


16  

Most logically:

大多数逻辑:

if not 'number' in request.POST:

Python convention:

Python公约:

if 'number' not in request.POST:

Both work in exactly the same way.

两者的工作方式完全相同。

#3


3  

What I have used many times is the following in my view:

在我看来,我多次使用的是以下几点:

def some_view(request):

    foobar = False

    if request.GET.get('foobar'):
        foobar = True

    return render(request, 'some_template.html',{
        'foobar': foobar,
    })

Then, in my template I can use the following URL syntax to set foobar:

然后,在我的模板中,我可以使用下面的URL语法来设置foobar:

<a href="{% url 'view_name_in_urls' %}?foobar=True">Link Name</a>

Also, since we returned the foobar variable from the view above, we can use that in the template with other logic blocks (great for navigation!):

另外,由于我们从上面的视图返回了foobar变量,所以我们可以在模板中使用它与其他逻辑块(非常适合导航!)

<li class="nav-item">
    {% if foobar %}
        <a class="nav-link active" ....
    {% else %}
        <a class="nav-link" ....
    {% endif %}
</li>

Hope it helps,

希望它可以帮助,

#1


32  

@Thomas gave you the generic way, but there is a shortcut for the particular case of getting a default value when a key does not exist.

@Thomas为您提供了通用的方法,但是在不存在密钥时,有一个获取默认值的特殊情况的快捷方式。

number = request.POST.get('number', 0)

This is equivalent to:

这相当于:

if 'number' not in request.POST:
    number = 0
else:
    number = request.POST['number']

#2


16  

Most logically:

大多数逻辑:

if not 'number' in request.POST:

Python convention:

Python公约:

if 'number' not in request.POST:

Both work in exactly the same way.

两者的工作方式完全相同。

#3


3  

What I have used many times is the following in my view:

在我看来,我多次使用的是以下几点:

def some_view(request):

    foobar = False

    if request.GET.get('foobar'):
        foobar = True

    return render(request, 'some_template.html',{
        'foobar': foobar,
    })

Then, in my template I can use the following URL syntax to set foobar:

然后,在我的模板中,我可以使用下面的URL语法来设置foobar:

<a href="{% url 'view_name_in_urls' %}?foobar=True">Link Name</a>

Also, since we returned the foobar variable from the view above, we can use that in the template with other logic blocks (great for navigation!):

另外,由于我们从上面的视图返回了foobar变量,所以我们可以在模板中使用它与其他逻辑块(非常适合导航!)

<li class="nav-item">
    {% if foobar %}
        <a class="nav-link active" ....
    {% else %}
        <a class="nav-link" ....
    {% endif %}
</li>

Hope it helps,

希望它可以帮助,