在Python中,如果auth_user_id不是None,则无法匹配None?

在Python中,如果auth_user_id不是None,则无法匹配None?,python,nonetype,Python,Nonetype,当变量为None时,如何阻止代码执行?我试过很多东西。这是我现在的化身: post_body = request.POST auth_user_id = post_body.get("auth_user_id", None) if auth_user_id is not None: print(auth_user_id) search_user = SearchUser.objects.get(user_id=auth_user_id) 现在,此代码在最后一行消失,并出现以下

当变量为None时,如何阻止代码执行?我试过很多东西。这是我现在的化身:

post_body = request.POST
auth_user_id = post_body.get("auth_user_id", None)

if auth_user_id is not None:
    print(auth_user_id)
    search_user = SearchUser.objects.get(user_id=auth_user_id)
现在,此代码在最后一行消失,并出现以下错误:

ValueError: invalid literal for int() with base 10: 'None'
这是:

        print(auth_user_id)
给我看“没有”


那么到底发生了什么?如何停止None上的代码

显然,您在这里得到的是文本字符串
'None'
,而不是
None
对象-请注意错误消息的区别,当传递
None
时,您会得到
TypeError
,当传递
'None'
时,您会得到一个ValueError:

# py2.7

>>> int(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'NoneType'
>>> int('None')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'None'


# py3
>>> int(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
>>> int('None')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'None'
#py2.7
>>>int(无)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:int()参数必须是字符串或数字,而不是“NoneType”
>>>int('None')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:基数为10的int()的文本无效:“无”
#py3
>>>int(无)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:int()参数必须是字符串、类似字节的对象或数字,而不是“NoneType”
>>>int('None')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:基数为10的int()的文本无效:“无”

您的问题来自HTTP请求正文(因为它是一篇文章),其中包含“auth\u user\u id”键的文本字符串
'None'
。请注意,使用带有适当验证的Django表单可以避免此问题

显然,您在这里得到的是文本字符串
'None'
,而不是
None
对象-请注意错误消息的区别,当传递
None
时,您会得到
TypeError
,当传递
'None'
时,您会得到一个ValueError:

# py2.7

>>> int(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'NoneType'
>>> int('None')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'None'


# py3
>>> int(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
>>> int('None')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'None'
#py2.7
>>>int(无)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:int()参数必须是字符串或数字,而不是“NoneType”
>>>int('None')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:基数为10的int()的文本无效:“无”
#py3
>>>int(无)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:int()参数必须是字符串、类似字节的对象或数字,而不是“NoneType”
>>>int('None')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:基数为10的int()的文本无效:“无”

您的问题来自HTTP请求正文(因为它是一篇文章),其中包含“auth\u user\u id”键的文本字符串
'None'
。请注意,使用带有适当验证的Django表单可以避免此问题

可能
None
是字符串。打印的输出是什么(类型(身份验证用户id))?它是
?@sKwa不需要问,它显然是一个字符串,否则OP会得到
类型错误
而不是
值错误
可能
是字符串。打印的输出是什么(类型(身份验证用户id))?它是
?@sKwa不需要问,它是一个字符串,很明显,否则OP会得到
类型错误
而不是
值错误