There is a form with two drop-down lists and one button. By clicking on the button, the data is sent in the form of json, filtering is performed on the basis of this data, then the filtered data is returned to the client. However, instead of the filtered data, error 500 (Internal Server Error) is displayed. The following output is displayed in traceback: string indices must be integers . How to fix this error?

Representation:

  function GetSelectValue() { var select_type = document.getElementById("type"); var type = select_type.value; var select_net = document.getElementById("net"); var net = select_net.value; var filter = JSON.stringify({type:type, net:net}); return filter } $(function () { $("#btn1").click(function () { var filter = GetSelectValue(); console.log(filter); $.ajax({ type: "GET", dataType: 'json', url: '/map/filter/', data: { 'filter': filter }, success: function (stations) { pointLayer.removeAll(); draw_point_layer(stations); }, error: function (xhr, status, error) { console.log(error); } }) }); }); <body> <div name="infoDiv" id="infoDiv" class="esri-widget"> <!--<b>Filter by geometry</b><br/><br/>--> <label for="net">Π‘Π΅Ρ‚ΡŒ:</label> <select id="net" class="options"> <option value="INTERMAGNET">INTERMAGNET</option> <option value="IMAGE">IMAGE</option> <option value="EISCAT">EISCAT</option> <option value="MACCS">MACCS</option> <option value="CARISMA">CARISMA</option> <option value="CANMOS">CANMOS</option> <option value="AUTUMNX">AUTUMNX</option> <option value="USGS">USGS</option> <option value="AMN">AMN</option> <option value="DTU">DTU</option> <option value="ENIGMA">ENIGMA</option> <option value="EMMA/PLASMON">EMMA/PLASMON</option> <option value="Japan Meteorological Agency">Japan Meteorological Agency</option> <option value="GFZ">GFZ</option> <option value="Italian Magnetic Network">Italian Magnetic Network</option> <option value="Mid-continent Magnetoseismic Chain">Mid-continent Magnetoseismic Chain</option> <option value="Russian Arctic and Antarctic Magnetometer">Russian Arctic and Antarctic Magnetometer</option> <option value="Finnish Meteorological Institute">Finnish Meteorological Institute</option> <option value="Swedish Institute of Space Physics">Swedish Institute of Space Physics</option> <option value="BAS">BAS</option> <option value="Российско-украинский сСгмСнт Π³Π΅ΠΎΠΌΠ°Π³Π½ΠΈΡ‚Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ…">Российско-украинский сСгмСнт Π³Π΅ΠΎΠΌΠ°Π³Π½ΠΈΡ‚Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ…</option> <option value="all" selected>всС</option> </select> <label for="type">Π’ΠΈΠΏ:</label> <select id="type" class="options"> <option value="station">станция</option> <option value="observatory">обсСрватория</option> <option value="all" selected>всС</option> </select ><br/><br/> <button id="btn1" class="esri-button" id="clearFilter" type="button"> ΠŸΡ€ΠΈΠΌΠ΅Π½ΠΈΡ‚ΡŒ </button> </div> </body> 

views.py

 from django.shortcuts import render from django.views.generic import TemplateView from .models import Station from django.http import HttpResponse from django.core import serializers def filter(request): if request.GET: filter = request.GET.get('filter') type = filter['type'] if type == 'all': stations = serializers.serialize('json', Station.objects.all()) else: stations = serializers.serialize('json', Station.objects.filter(type=type)) return HttpResponse(stations, content_type='application/json') 
  • one
    Read the server logs, they will definitely write the reason for the error - andreymal
  • one
    And traysbek for whom it is thought up? - m0nte-cr1st0

1 answer 1

I got rid of this error by applying the json.loads() method. Modified views.py will look like this.

  from django.shortcuts import render from django.views.generic import TemplateView from .models import Station from django.http import HttpResponse from django.core import serializers import json def filter(request): if request.GET: filter = request.GET.get('filter') decoded_filter = json.loads(filter) type = decoded_filter["type"] if type == 'all': stations = serializers.serialize('json', Station.objects.all()) else: stations = serializers.serialize('json', Station.objects.filter(type=type)) return HttpResponse(stations, content_type='application/json')