Bottom Page

Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 KeyError
#1
Hello to everyone, i am new on python and django and i have a problem. I have a view that returns network asset.
In other views with similar data work but not in this not.
This is my code:
@login_required
def activos_mostrar(request):
    if request.method == 'GET':
        obj_activos = Activos.objects.values("id", "nombre", "direccion_ip", "tipos_dispositivos", "sistema_operativo", "valor_activo")
        obj_activos_temp = {}
        obj_activos_temp['draw'] = request.GET['draw']
        obj_activos_temp['recordsTotal'] = len(obj_activos)
        obj_activos_temp['recordsFiltered'] = len(obj_activos)
        if len(obj_activos) != 0:
            for temp in obj_activos:
                obj_activos_temp['data'].append({'0': "<img src=\" \# \" class= \" ui-icon ui-icon-circle-plus \" >", '1': temp.nombre, '2': temp.direccion_ip, '3': temp.tipos_dispositivos, '4': temp.sistema_operativo, '5': temp.valor_activo, '6': "no", '7': "no", 'extra': temp.id})
        else:
            obj_activos_temp['data'] = ""
        return JsonResponse(obj_activos_temp)
In the web page he said "KeyError at /activos/mostrar/
'data'"
Quote
#2
Perhaps the key is not 'data', Capitalization? or data_something_else?
Quote
#3
No, this is all my code.
Quote
#4
do you need
obj_activos_temp['data'].append = ""
?
Quote
#5
why?? could you explain me?
Quote
#6
Sorry, it's early and I didn't look deeply enough at your code.
Is the error on line 11 or 13?
It would be helpful to see the error traceback verbatim
Quote
#7
This is the only error:
KeyError at /activos/mostrar/
'data'

Request Method: GET
Request URL: http://192.168.2.5/activos/mostrar/?draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=false&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=4&columns%5B4%5D%5Bname%5D=&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=true&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B5%5D%5Bdata%5D=5&columns%5B5%5D%5Bname%5D=&columns%5B5%5D%5Bsearchable%5D=true&columns%5B5%5D%5Borderable%5D=true&columns%5B5%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B5%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=1&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1506945121717
Django Version: 1.10.7
Python Executable: /usr/bin/python3
Python Version: 3.5.3
Python Path: ['/home/linursiem', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
Server time: Lun, 2 Oct 2017 07:52:03 -0400
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'sitetree',
 'seguridad',
 'activos']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback:  

File "/usr/lib/python3/dist-packages/django/core/handlers/exception.py" in inner
  42.             response = get_response(request)

File "/usr/lib/python3/dist-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/lib/python3/dist-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python3/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/home/linursiem/activos/views.py" in activos_mostrar
  27.                 obj_activos_temp['data'].append({'0': "<img src=\" \# \" class= \" ui-icon ui-icon-circle-plus \" >", '1': temp.nombre, '2': temp.direccion_ip, '3': temp.tipos_dispositivos, '4': temp.sistema_operativo, '5': temp.valor_activo, '6': "no", '7': "no", 'extra': temp.id})

Exception Type: KeyError at /activos/mostrar/
Exception Value: 'data'
Quote
#8
well, on line 5 obj_activos_temp is empty dict, then on lines 6-8 you set the items for keys 'draw', 'recordsTotal', 'recordsFiltered'. You never set the item of key 'data' to be empty list in order to be able to append to it afterwards on line 11. Either set the item or use dict.setdefault method to set empty list along with the append.
nilamo likes this post
Quote
#9
Thanks' it's work.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)