当变量为None时,如何停止执行代码?我尝试了很多东西。这是我目前的化身:
create function concatall (
@fn as TEXT,
@mn as TEXT,
@ln as TEXT
) returns @merged table (
[FullName] TEXT
)
as
begin
insert into @merged
SELECT CONCAT(@fn , @mn , @ln)
return;
end
现在这段代码在最后一行死掉了这个错误:
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'
显示“无”。
那是怎么回事?如何停止无代码?
答案 0 :(得分:4)
显然,你在这里得到的是文字字符串'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'
您的问题来自HTTP请求正文(因为它是一个POST),其中包含“auth_user_id”键的文字字符串'None'
。请注意,使用具有适当验证的Django表单可以避免此问题。