Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django中的逻辑NOT操作_Python_Django_Logical Operators - Fatal编程技术网

Python django中的逻辑NOT操作

Python django中的逻辑NOT操作,python,django,logical-operators,Python,Django,Logical Operators,我有一个查看功能,可以切换用户状态(活动-非活动): 如果当前_状态为真,则通过将其设为假来正常工作。但是如果当前\u状态为False,则它仍为False 我也尝试过打印(非当前状态),但令人惊讶的是,notfalse仍然是False 我不确定当您只需在用户上切换处于活动状态时,为什么需要当前状态: def toggle_user_state(request, user_id): user = get_object_or_404(User, pk=user_id) user.is

我有一个查看功能,可以切换用户状态(活动-非活动):

如果
当前_状态
为真,则通过将其设为假来正常工作。但是如果
当前\u状态
为False,则它仍为False


我也尝试过打印(非当前状态),但令人惊讶的是,notfalse仍然是False

我不确定当您只需在用户上切换
处于活动状态时,为什么需要
当前状态

def toggle_user_state(request, user_id):
    user = get_object_or_404(User, pk=user_id)
    user.is_active = not user.is_active    # take a NOT of active state here
    user.save()
    return HttpResponseRedirect(reverse('cdms:user_details', kwargs={'user_id': user.id}))

我不确定当您可以简单地在用户上切换
处于活动状态时,为什么需要
当前状态

def toggle_user_state(request, user_id):
    user = get_object_or_404(User, pk=user_id)
    user.is_active = not user.is_active    # take a NOT of active state here
    user.save()
    return HttpResponseRedirect(reverse('cdms:user_details', kwargs={'user_id': user.id}))

url捕获的
当前\u状态始终是字符串。因此,在您的情况下,它将是
“True”
“False”

一个解决方案是:

if current_state == "True":
    user.is_active = False
elif current_state == "False":
    user.is_active = True
# Define a function to the outer scope

def str_to_bool(s):
    if s == 'True':
         return True
    elif s == 'False':
         return False
    else:
         raise ValueError

# Then inside toggle_user_state do this
try:
    user.is_active = not str_to_bool(current_state)
except ValueError:
    # handle error here (its neither "True" or "False")
else:
    # everything worked. Continue
另一个解决方案是:

if current_state == "True":
    user.is_active = False
elif current_state == "False":
    user.is_active = True
# Define a function to the outer scope

def str_to_bool(s):
    if s == 'True':
         return True
    elif s == 'False':
         return False
    else:
         raise ValueError

# Then inside toggle_user_state do this
try:
    user.is_active = not str_to_bool(current_state)
except ValueError:
    # handle error here (its neither "True" or "False")
else:
    # everything worked. Continue

url捕获的
当前\u状态始终是字符串。因此,在您的情况下,它将是
“True”
“False”

一个解决方案是:

if current_state == "True":
    user.is_active = False
elif current_state == "False":
    user.is_active = True
# Define a function to the outer scope

def str_to_bool(s):
    if s == 'True':
         return True
    elif s == 'False':
         return False
    else:
         raise ValueError

# Then inside toggle_user_state do this
try:
    user.is_active = not str_to_bool(current_state)
except ValueError:
    # handle error here (its neither "True" or "False")
else:
    # everything worked. Continue
另一个解决方案是:

if current_state == "True":
    user.is_active = False
elif current_state == "False":
    user.is_active = True
# Define a function to the outer scope

def str_to_bool(s):
    if s == 'True':
         return True
    elif s == 'False':
         return False
    else:
         raise ValueError

# Then inside toggle_user_state do this
try:
    user.is_active = not str_to_bool(current_state)
except ValueError:
    # handle error here (its neither "True" or "False")
else:
    # everything worked. Continue

不假总是真的

>>> not False
>>> True
非“False”将始终返回False

>>> not 'False'
>>> False
原因是,任何非空字符串的计算结果都是布尔值True

>>> if 'False':
>>>    print 'False in string but not boolean False'
>>> 'False in string but not boolean False'
作为重述字符串,“False”不等于bool False

这里我通常要做的是编写一个truthy函数,将任何可能的true或false的预期含义转换为布尔值

def is_true(value):
    if value in ['1', 1, True, 'true', 'yes', 'Yes', 'True']:
        return True
    return False
所以现在你可以做了

def toggle_user_state(request, user_id, current_state):
    user = get_object_or_404(User, pk=user_id)
    current_state = is_true(current_state) # current_state will now be boolean
    user.is_active = not current_state # now, will be boolean opposite
    user.save()
    return HttpResponseRedirect(reverse('cdms:user_details', kwargs={'user_id': user.id}))

不假总是真的

>>> not False
>>> True
非“False”将始终返回False

>>> not 'False'
>>> False
原因是,任何非空字符串的计算结果都是布尔值True

>>> if 'False':
>>>    print 'False in string but not boolean False'
>>> 'False in string but not boolean False'
作为重述字符串,“False”不等于bool False

这里我通常要做的是编写一个truthy函数,将任何可能的true或false的预期含义转换为布尔值

def is_true(value):
    if value in ['1', 1, True, 'true', 'yes', 'Yes', 'True']:
        return True
    return False
所以现在你可以做了

def toggle_user_state(request, user_id, current_state):
    user = get_object_or_404(User, pk=user_id)
    current_state = is_true(current_state) # current_state will now be boolean
    user.is_active = not current_state # now, will be boolean opposite
    user.save()
    return HttpResponseRedirect(reverse('cdms:user_details', kwargs={'user_id': user.id}))

尝试同时打印
curernu状态
非当前状态
。这听起来可能很愚蠢,但您是否在
当前状态
中使用布尔运算符或字符串?尝试同时打印
curernu状态
非当前状态
。这听起来可能很愚蠢,但是您是否在
当前状态下使用布尔运算符或字符串?
?这在确定问题根源方面是正确的,但我认为AKS的答案更好,因为它提供了一种更直接的方法来实现此功能。@Vasilyalexev。当然我之所以发布它,是因为OP将
当前_状态
作为url参数传递。如果没有它,同样的东西也可以工作,那么AKS的答案更好!谢谢你的回答。你已经表明了我的错误。从url捕获的当前_状态是一个字符串。这在确定问题根源方面是正确的,但我认为AKS的答案更好,因为它提供了一种更直接的方法来实现此功能。@Vasilyalexev。当然我之所以发布它,是因为OP将
当前_状态
作为url参数传递。如果没有它,同样的东西也可以工作,那么AKS的答案更好!谢谢你的回答。你已经表明了我的错误。从url捕获的当前_状态是一个字符串。感谢您的回答。是的,我没有额外的参数。谢谢你的回答。是的,我没有额外的参数。