Skip to content

Instantly share code, notes, and snippets.

@mayroncachina
Last active August 29, 2015 13:56
Show Gist options
  • Save mayroncachina/8960384 to your computer and use it in GitHub Desktop.
Save mayroncachina/8960384 to your computer and use it in GitHub Desktop.
Simple Json Sample
from datetime import datetime
import json
data = {
'title': 'Hello World',
'created_on': datetime(1986, 5, 16, 0, 0)
}
def date_handler(obj):
return obj.isoformat() if hasattr(obj, 'isoformat') else obj
print json.dumps(data, default=date_handler)
----------------------------
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from django.utils import simplejson
from inboxtv.box.models import Evento, Imagem
from datetime import datetime
import json
def date_handler(obj):
return obj.isoformat() if hasattr(obj, 'isoformat') else obj
def home(request):
evento = Evento.objects.filter(id=1).values_list()
imagens = Imagem.objects.filter(evento=1).values_list()
data = {
"evento": list(evento),
"imagens": list(imagens)
}
return json.dumps(data, default=date_handler)
#return HttpResponse(simplejson.dumps(to_json), mimetype='application/json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment