Skip to content

Instantly share code, notes, and snippets.

@allieus
Last active December 10, 2015 00:29
Show Gist options
  • Save allieus/4351558 to your computer and use it in GitHub Desktop.
Save allieus/4351558 to your computer and use it in GitHub Desktop.
Django 에서 json.JSONEncoder 에 datetime 형식 추가
import json
from datetime import datetime
from django.http import Http404
from django.http import HttpResponse
class DateTimeJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%d %H:%M') # FORMAT
else:
return super(DateTimeJSONEncoder, self).default(obj)
def json_encode(data):
return DateTimeJSONEncoder().encode(data)
def view_function(request):
data = {}
json_string = json_encode(data)
return HttpResponse(json_string, mimetype='application/javascript')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment