Skip to content

Instantly share code, notes, and snippets.

@elpargo
Last active December 18, 2015 10:58
Show Gist options
  • Save elpargo/5771800 to your computer and use it in GitHub Desktop.
Save elpargo/5771800 to your computer and use it in GitHub Desktop.
Initial version of provinciasapp in django (won't run just for showing purposes) repo coming.... This behaves like the data repository. That is asking for the data structure will bring all the json below it. ie: ```bash http://127.0.0.1:8000/ -> all data http://127.0.0.1:8000/1 -> all data for country 1 http://127.0.0.1:8000/1/Provincia/ -> all …
from django.http import HttpResponse
from django.utils import simplejson as json
from django.views.generic.list import BaseListView
def json_render_to_response(context):
"Returns a JSON response containing 'context' as payload"
payload = json.dumps(context)
json_response = HttpResponse(payload,content_type="application/json")
return json_response
def get_json():
with open("provincias.json") as f:
data = json.loads(f.read())
return data
def get_anything(params):
#TODO: add error handling
data = get_json()
for level in params:
data = data[level]
return data
def provincias(request):
c = request.path.split('/')
c = filter(None,c) #eliminate the first and last empty path items
c = get_anything(c)
return json_render_to_response(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment