1

I am trying to pass data from an api through Django views.py into my html page.

I have the error above, have read similar threads ie tuple() the data, however I cannot pass it no matter what I try. If anyone is aware of a solution - thanks.

views.py:

def forces(request):
    import requests
    import json

    api_request = requests.get("https://data.police.uk/api/forces")

    try:
        api = json.loads(api_request.content)
        api = tuple(api)
    except Exception as e:
        api = "Error getting api forces data, sorry!"

    return render(request,'forces.html', {'api', api})

The data sample:

[{"id":"avon-and-somerset","name":"Avon and Somerset Constabulary"},{"id":"bedfordshire","name":"Bedfordshire Police"},{"id":"cambridgeshire","name":"Cambridgeshire Constabulary"},{"id":"cheshire","name":"Cheshire Constabulary"},{"id":"city-of-london","name":"City of London Police"},{"id":"cleveland","name":"Cleveland Police"},{"id":"cumbria","name":"Cumbria Constabulary"},{"id":"derbyshire","name":"Derbyshire Constabulary"},{"id":"devon-and-cornwall","name":"Devon & Cornwall Police"},{"id":"dorset","name":"Dorset Police"},{"id":"durham","name":"Durham Constabulary"},{"id":"dyfed-powys","name":"Dyfed-Powys Police"},{"id":"essex","name":"Essex Police"},{"id":"gloucestershire","name":"Gloucestershire Constabulary"},{"id":"greater-manchester","name":"Greater Manchester Police"},{"id":"gwent","name":"Gwent Police"},{"id":"hampshire","name":"Hampshire Constabulary"},{"id":"hertfordshire","name":"Hertfordshire Constabulary"},{"id":"humberside","name":"Humberside Police"},{"id":"kent","name":"Kent Police"},{"id":"lancashire","name":"Lancashire Constabulary"},{"id":"leicestershire","name":"Leicestershire Police"},{"id":"lincolnshire","name":"Lincolnshire Police"},{"id":"merseyside","name":"Merseyside Police"},{"id":"metropolitan","name":"Metropolitan Police Service"},{"id":"norfolk","name":"Norfolk Constabulary"},{"id":"north-wales","name":"North Wales Police"},{"id":"north-yorkshire","name":"North Yorkshire Police"},{"id":"northamptonshire","name":"Northamptonshire Police"},{"id":"northumbria","name":"Northumbria Police"},{"id":"nottinghamshire","name":"Nottinghamshire Police"},{"id":"northern-ireland","name":"Police Service of Northern Ireland"},{"id":"south-wales","name":"South Wales Police"},{"id":"south-yorkshire","name":"South Yorkshire Police"},{"id":"staffordshire","name":"Staffordshire Police"},{"id":"suffolk","name":"Suffolk Constabulary"},{"id":"surrey","name":"Surrey Police"},{"id":"sussex","name":"Sussex Police"},{"id":"thames-valley","name":"Thames Valley Police"},{"id":"warwickshire","name":"Warwickshire Police"},{"id":"west-mercia","name":"West Mercia Police"},{"id":"west-midlands","name":"West Midlands Police"},{"id":"west-yorkshire","name":"West Yorkshire Police"},{"id":"wiltshire","name":"Wiltshire Police"}]
Siimm Kahn
  • 110
  • 2
  • 10
  • Does this answer your question? [How can I convert a dictionary into a list of tuples?](https://stackoverflow.com/questions/674519/how-can-i-convert-a-dictionary-into-a-list-of-tuples) – Kent Shikama Jul 27 '20 at 23:16
  • I tried that i get the error: context must be a dict rather than set. – Siimm Kahn Jul 27 '20 at 23:28

2 Answers2

1

It would be better to extract the json response using .json()

api = api_request.json()
Mark Rofail
  • 808
  • 1
  • 8
  • 18
1

SOLVED: Syntax issue, below solution.

return render(request,'forces.html', {'api' : api})
Siimm Kahn
  • 110
  • 2
  • 10