Why Autocomplete form in django doesn't work

I have been trying several different kind of code for an autocomplte form with Django Im new on this but here are the code:

Views.py

def search_ifsc(request):
    try:
        q = request.GET.get('q', '').capitalize()
        search_qs = InfoTrabajadores.objects.filter(documento__startswith=q)
        results = []
        print(q)
        for r in search_qs:
            dict_data = {'documento':r.documento,'nombres':r.nombres,'info':{
            'num_emergencia':r.num_emergencia,
            'prov_salud':r.prov_salud,
            'prov_salud_trabj':r.prov_salud_trabj,
            'rh':r.rh}}
            results.append(dict_data)
        data = json.dumps(results)
    except Exception as e:
        data = 'fail'+ f'\n{e}'

    mimetype = 'application/json'
    return HttpResponse(data, mimetype)

This in the endpoint to filter te data with the document Inside the document:

 <script type='text/javascript'>

  fetch('http://127.0.0.1:8000/ajax/search/')
  .then((response) => response.json())
  .then((data) => {
       document.getElementById('nombre').value = data.nombres;
      document.getElementById('num_emergencia').value = data.num_emergencia;
      document.getElementById('prov_salud').value = data.prov_salud;
      document.getElementById('prov_salud_trabj').value = data.prov_salud_trabj;
      document.getElementById('rh').value = data.rh;
    } )

And the urls.py

urlpatterns = [
    path('ajax/search/' , search_ifsc, name='search_view'),
] 

This is how the site looks with the actual code

I've been trying change de model and how the query works, but nothing change the response in the site

Back to Top