Precious11
Precious11

Reputation: 31

TypeError NoneType in Python script when running Django application

I get the error: TypeError: 'NoneType' object is not subscriptable. You can view the code below. The strange thing is when I run the code in Notebook it works. But when I run this in Django I get this NoneType error in return. I need this code in my Django application, so can someone please help.

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"})

Below you can find the full error traceback:

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

Upvotes: 0

Views: 59

Answers (1)

Maxim Danilov
Maxim Danilov

Reputation: 3360

The [...] indexing syntax is called a subscript.

I your case or changed_id is None or data is None. Of cause: None[-1], for example, or None['year'] is not subscriptable.

Your example don't answer, where you get the pandas.dataframe data, or how you receive changed_id, probably, this is not the whole code.

Upvotes: 1

Related Questions