Creating Rest Api in python using JSON?
<p>I'm new to python REST API and so facing some particular problems. I want that when I enter the input as pathlabid(primary key), I want the corresponding data assigned with that key as output. When I run the following code i only get the data corresponding to the first row of table in database even when the id i enter belong to some other row.
This is the VIEWS.PY </p>

<pre><code>class pathlabAPI(View):
@csrf_exempt
def dispatch(self, *args, **kwargs):
    # dont worry about the CSRF here
    return super(pathlabAPI, self).dispatch(*args, **kwargs)

def post(self, request):
    post_data = json.loads(request.body)
    Pathlabid = post_data.get('Pathlabid') or ''
    lablist = []
    labdict = {}

    lab = pathlab()
    labs = lab.apply_filter(Pathlabid = Pathlabid)


    if Pathlabid :


        for p in labs:
                labdict["Pathlabid"] = p.Pathlabid
                labdict["name"] = p.Name
                labdict["email_id"] = p.Emailid
                labdict["contact_no"] = p.BasicContact
                labdict["alternate_contact_no"] = p.AlternateContact
                labdict["bank_account_number"] = p.Accountnumber
                labdict["ifsccode"] = p.IFSCcode
                labdict["country"] = p.Country
                labdict["homepickup"] = p.Homepickup
                lablist.append(labdict)
                return HttpResponse(json.dumps(lablist))
    else:

        for p in labs:
                labdict["bank_account_number"] = p.Accountnumber
                lablist.append(labdict)
                return HttpResponse(json.dumps(lablist))
</code></pre>

<p>You are return the response in for loop so that loop break on 1st entry </p>

<pre><code>import json
some_list = []
for i in data:
    some_list.append({"key": "value"})
return HttpResponse(json.dumps({"some_list": some_list}), content_type="application/json")
</code></pre>

<p>Try above example to solve your problem</p>

python json django rest api