Django运行Python脚本报TypeError: 'NoneType'下标访问错误
问题描述
Django环境集成Plotly Dash业务代码运行时触发报错:TypeError: 'NoneType' object is not subscriptable。同一段代码在Notebook环境中可正常执行,迁移到Django应用后触发该NoneType类型错误,需在Django中正常集成运行该段代码。
问题业务代码
def update_pie(year, changed_id): dff = data[data['year'] == year] if changed_id[-1] == "." or changed_id[-1] == "1": dff = dff.groupby('air quality', as_index=False).count() dff = dff.rename(columns = {"country": "count"}) elif changed_id[-1] != "1" and changed_id[-1] != ".": dff = dff[dff['continent code'] == int(changed_id[-1]) - 1] dff = dff.groupby('air quality', as_index=False).count() dff = dff.rename(columns = {"country": "count"})
错误回溯栈
Traceback (most recent call last): File "/mnt/c/Users/Gebruiker/project/templication/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) File "/mnt/c/Users/Gebruiker/project/templication/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/mnt/c/Users/Gebruiker/project/templication/env/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/mnt/c/Users/Gebruiker/project/templication/env/lib/python3.8/site-packages/django_plotly_dash/views.py", line 74, in update return _update(request, ident, stateless, **kwargs) File "/mnt/c/Users/Gebruiker/project/templication/env/lib/python3.8/site-packages/django_plotly_dash/views.py", line 91, in _update resp = app.dispatch_with_args(request_body, arg_map) File "/mnt/c/Users/Gebruiker/project/templication/env/lib/python3.8/site-packages/django_plotly_dash/dash_wrapper.py", line 698, in dispatch_with_args res = callback(*args, **{k: v for k, v in argMap.items() if k in parameters_to_inject}) File "/mnt/c/Users/Gebruiker/project/templication/env/lib/python3.8/site-packages/dash/_callback.py", line 151, in add_context output_value = func(*func_args, **func_kwargs) # %% callback invoked %% File "/mnt/c/Users/Gebruiker/project/templication/dashtemplates/dashboards/dashboard_four/fourthdash.py", line 140, in update_map if changed_id[-1] == "." or changed_id[-1] == "1": TypeError: 'NoneType' object is not subscriptable
根因定位
报错直接触发点是代码行if changed_id[-1] == "." or changed_id[-1] == "1":,错误提示明确说明当前操作的changed_id是None类型,None值不支持下标索引操作,因此抛出类型错误。
Notebook环境运行正常是因为本地测试时手动传入的changed_id始终是有效字符串值,从未传入None;而django-plotly-dash的Web运行逻辑中,Dash应用首次页面加载、触发组件未产生实际交互值时,回调函数的入参默认会传入None,原代码没有做入参校验,直接对None取下标就会触发报错。
修复方案
- 在函数入口增加入参空值兜底逻辑,拦截
changed_id为None的场景,匹配原有业务逻辑给默认值,首次加载默认走全局空气质量统计分支即可。 - 优化冗余判断逻辑,提前提取
changed_id最后一位字符,避免重复写取下标的操作,同时兼容边界输入场景。
修复后的参考代码:
def update_pie(year, changed_id): # 空值兜底:首次加载changed_id为None时,默认赋值为"."走全局统计逻辑 if not changed_id: changed_id = "." dff = data[data['year'] == year] last_char = changed_id[-1] if last_char == "." or last_char == "1": dff = dff.groupby('air quality', as_index=False).count() dff = dff.rename(columns = {"country": "count"}) else: dff = dff[dff['continent code'] == int(last_char) - 1] dff = dff.groupby('air quality', as_index=False).count() dff = dff.rename(columns = {"country": "count"}) # 原代码漏写返回值,Web回调需要明确返回计算后的数据 return dff
补充说明:Web环境下的接口入参永远存在空值、格式不符的可能,和Notebook中手动固定传参的理想测试环境差异很大,所有直接操作入参下标、类型转换的逻辑前,都应该先做合法性校验,避免这类类型错误。
内容的提问来源于stack exchange,提问作者Precious11
相关产品推荐
相关产品推荐

