Filter records according to zone (user table field) user

Without seeing the sending part to the button, with the current code it does not list the records.

console error

UnboundLocalError: cannot access local variable 'instance' where it is not associated with a value

Please post the complete traceback.

Internal Server Error: /reclamaciones/listado_reclamaciones/
Traceback (most recent call last):
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\reclamaciones\views.py", line 63, in get
    return HttpResponse(serialize('json',self.get_queryset()),'application/json')
                                         ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\reclamaciones\views.py", line 59, in get_queryset
    return instance
           ^^^^^^^^
UnboundLocalError: cannot access local variable 'instance' where it is not associated with a value

Sorry, my error. That statement should be return queryset.

Now yes. Well, the next step would be to send the instance.pk to my button, is that right?
It would be to pass an instance to the window.location.href parameter of my button in some way.

Nope, that’s not correct.

You’ve added ‘url’ as a field in your queryset. That field is the complete url, with the pk reference. You now access that field in exactly the same way as you access every other field.

Look at what is being returned to the browser in your browser’s developer tools.

I don’t see that it returns the url, it does return the registry data, the pk to which it corresponds and the model.

Please show a section of the data that is being returned to the browser, looking at the network tab of the browser’s developer tools.

You mean this?

I tried to make some change but the same thing comes out, as I was saying, I don’t see the part of the url that we have assigned to each user

Quick update, I know basically what’s not happening here, I’m trying to find a good solution.

I had also read the option to have all the buttons you need, in my case there would be three (edit, send and approve) and hide the button depending on the user. I don’t know if it could be done like that…

Ok, I’ve tried approaching this with recommending as few changes as possible, but what I think the easiest way to handle this isn’t going to facilitate that.

What I’m thinking your best approach for the view is going to look more like this:

class ListadoReclamacion(ListView):
   model = Rec

   def get_queryset(self):
       if self.request.user.cod_cargo_id==3:
           queryset = self.model.objects.filter(estado_cod=4,del_dep_code=self.request.user.cod_delegacion_zona.cod_delegacion)
       else:
           queryset = self.model.objects.filter(estado_cod=1,del_dep_code=self.request.user.cod_delegacion_zona.cod_delegacion)
       
       self.dict_data = queryset.values()
       for instance in self.dict_data:
           if self.request.user.cod_cargo_id==3:
               instance.url = reverse('aprobar_reclamacion', args=[instance.pk])
           else:
               instance.url = reverse('enviar_para_aprobar', args=[instance.pk])
       return queryset

   def get(self,request,*args,**kwargs):
       if request.headers.get('x-requested-with') == 'XMLHttpRequest':
           return JsonResponse(self.dict_data, safe=False)
       else:
           return redirect('inicio_reclamaciones')

(Note, I’m winging this a bit - there may be a syntax error or 10 inside this code.)

I imagine it must be complicated.
I have tried and I get the following:

Traceback (most recent call last):
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\reclamaciones\views.py", line 64, in get
    return JsonResponse(self.dict_data, safe=False)
                        ^^^^^^^^^^^^^^
AttributeError: 'ListadoReclamacion' object has no attribute 'dict_data'

Yes, I overlooked that you’re not calling super in your get function, which means get_queryset isn’t being called by default.

Insert the line below after the get:

Traceback (most recent call last):
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\reclamaciones\views.py", line 63, in get
    self.get_queryset()
  File "C:\Users\apenaranda\Desktop\Python_Intranet\reclamaciones\views.py", line 57, in get_queryset
    instance.url = reverse('aprobar_reclamacion', args=[instance.pk])
                                                        ^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'pk'

args=[instance.pk]args=[instance['id']] - both occurrances

modify this

instance.url = reverse('aprobar_reclamacion', args=[instance.pk])

For this?
instance.url = reverse(‘aprobar_reclamacion’, args=[instance.pk]args=[instance['id']])

No, you’re only replacing the args parameter.
args should be instance['id'], not instance.pk.

Fit as you said, I get the following in the console:

Internal Server Error: /reclamaciones/listado_reclamaciones/
Traceback (most recent call last):
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\reclamaciones\views.py", line 63, in get
    self.get_queryset()
  File "C:\Users\apenaranda\Desktop\Python_Intranet\reclamaciones\views.py", line 57, in get_queryset
    instance.url = reverse('aprobar_reclamacion', args=[instance['id']])
    ^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'url'